diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 9bf9bcc69..40c18915f 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -475,7 +475,7 @@ class AbstractTab(QWidget): # self.search = AbstractSearch(parent=self) # self.printing = AbstractPrinting() self.data = TabData() - self._layout = None + self._layout = miscwidgets.WrapperLayout(self) self._widget = None self._progress = 0 self._has_ssl_errors = False @@ -484,8 +484,8 @@ class AbstractTab(QWidget): def _set_widget(self, widget): # pylint: disable=protected-access - self._layout = miscwidgets.WrapperLayout(widget, self) self._widget = widget + self._layout.wrap(self, widget) self.history._history = widget.history() self.scroller._init_widget(widget) self.caret._widget = widget @@ -493,8 +493,6 @@ class AbstractTab(QWidget): self.search._widget = widget self.printing._widget = widget widget.mouse_wheel_zoom.connect(self.zoom._on_mouse_wheel_zoom) - widget.setParent(self) - self.setFocusProxy(widget) def _set_load_status(self, val): """Setter for load_status.""" diff --git a/qutebrowser/browser/inspector.py b/qutebrowser/browser/inspector.py index b3e3e0a74..ed2e4ecd1 100644 --- a/qutebrowser/browser/inspector.py +++ b/qutebrowser/browser/inspector.py @@ -59,14 +59,12 @@ class AbstractWebInspector(QWebInspector): def __init__(self, parent=None): super().__init__(parent) self._widget = None - self._layout = None + self._layout = miscwidgets.WrapperLayout(self) self._load_state_geometry() def _set_widget(self, widget): self._widget = widget - self._layout = miscwidgets.WrapperLayout(self._widget, self) - self.setFocusProxy(self._widget) - self._widget.setParent(self) + self._layout.wrap(self, widget) def _load_state_geometry(self): """Load the geometry from the state file.""" diff --git a/qutebrowser/misc/miscwidgets.py b/qutebrowser/misc/miscwidgets.py index 2ad184193..63a5718c6 100644 --- a/qutebrowser/misc/miscwidgets.py +++ b/qutebrowser/misc/miscwidgets.py @@ -235,9 +235,9 @@ class WrapperLayout(QLayout): easily be accidentally accessed. """ - def __init__(self, widget, parent=None): + def __init__(self, parent=None): super().__init__(parent) - self._widget = widget + self._widget = None def addItem(self, _widget): raise AssertionError("Should never be called!") @@ -254,3 +254,9 @@ class WrapperLayout(QLayout): def setGeometry(self, rect): self._widget.setGeometry(rect) + + def wrap(self, container, widget): + """Wrap the given widget in the given container.""" + self._widget = widget + container.setFocusProxy(widget) + widget.setParent(container) diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 5027f301b..7cb15c1c6 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -27,7 +27,7 @@ from unittest import mock from PyQt5.QtCore import pyqtSignal, QPoint, QProcess, QObject from PyQt5.QtNetwork import (QNetworkRequest, QAbstractNetworkCache, QNetworkCacheMetaData) -from PyQt5.QtWidgets import QCommonStyle, QLineEdit +from PyQt5.QtWidgets import QCommonStyle, QLineEdit, QWidget from qutebrowser.browser import browsertab from qutebrowser.browser.webkit import history @@ -253,6 +253,8 @@ class FakeWebTab(browsertab.AbstractTab): self._url = url self._progress = progress self.scroller = FakeWebTabScroller(self, scroll_pos_perc) + wrapped = QWidget() + self._layout.wrap(self, wrapped) def url(self): return self._url diff --git a/tests/unit/browser/test_tab.py b/tests/unit/browser/test_tab.py index ce882fcda..0bc240b45 100644 --- a/tests/unit/browser/test_tab.py +++ b/tests/unit/browser/test_tab.py @@ -96,7 +96,6 @@ def tab(request, default_config, qtbot, tab_registry, cookiejar_and_cache): def test_tab(qtbot, view, config_stub, tab_registry): tab_w = browsertab.AbstractTab(win_id=0) qtbot.add_widget(tab_w) - tab_w.show() assert tab_w.win_id == 0 assert tab_w._widget is None @@ -118,6 +117,9 @@ def test_tab(qtbot, view, config_stub, tab_registry): assert tab_w.history._history is view.history() assert view.parent() is tab_w + tab_w.show() + qtbot.waitForWindowShown(tab_w) + class TestJs: diff --git a/tests/unit/misc/test_miscwidgets.py b/tests/unit/misc/test_miscwidgets.py index 8e92176f9..728731971 100644 --- a/tests/unit/misc/test_miscwidgets.py +++ b/tests/unit/misc/test_miscwidgets.py @@ -89,8 +89,14 @@ class TestWrapperLayout: parent = QWidget() qtbot.add_widget(wrapped) qtbot.add_widget(parent) - miscwidgets.WrapperLayout(wrapped, parent) + layout = miscwidgets.WrapperLayout(parent) + layout.wrap(parent, wrapped) + parent.wrapped = wrapped return parent def test_size_hint(self, container): assert container.sizeHint() == QSize(23, 42) + + def test_wrapped(self, container): + assert container.wrapped.parent() is container + assert container.focusProxy() is container.wrapped