New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access QDockWidget instances from dock widgets #3659
Comments
Howdie @jwindhager 馃憢
can you clarify what you mean there? There's a little understandable semantic confusion here, I think of "dock widget" as actually the
what about
A QWidget can already connect to it's parent's events, for example: from qtpy.QtWidgets import QWidget, QMainWindow, QApplication, QDockWidget
from qtpy.QtCore import QObject, QEvent, Qt
class MyWidget(QWidget):
def __init__(self, parent=None) -> None:
super().__init__(parent)
self.installEventFilter(self)
print("during init, parent is", self.parent())
def eventFilter(self, obj: QObject, event: QEvent):
if event.type() == QEvent.ParentChange:
parent = self.parent()
print('parent Changed!, now:', parent)
if isinstance(parent, QDockWidget):
parent.dockLocationChanged.connect(
lambda area: print("changed location to", area)
)
return super().eventFilter(obj, event)
if __name__ == '__main__':
app = QApplication([])
wdg = MyWidget()
win = QMainWindow()
win.setCentralWidget(QWidget())
dock_widget = QDockWidget()
dock_widget.setWidget(wdg)
win.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, dock_widget)
win.show()
app.exec_() |
if the approach above doesn't work, all dock widgets are accessible in the private attribute |
Hi @tlambert03, thanks so much for the extensive reply!
Indeed, that's precisely what I meant - sorry for the confusion.
Yes, I ran in to the issue that |
This issue has been mentioned on Image.sc Forum. There might be relevant details there: https://forum.image.sc/t/composing-workflows-in-napari/61222/2 |
This issue has been mentioned on Image.sc Forum. There might be relevant details there: https://forum.image.sc/t/access-viewer-object-within-magicgui-widget-wrapper/61366/5 |
馃殌 Feature
Expose
QDockWidget
instances to dock widgetsMotivation
The original motivation for this was to adapt a widget's layout upon changing its dock location (e.g.
QVBoxLayout
for left/right,QHBoxLayout
for top/bottom). This would require listening to QDockWidget.dockLocationChanged. However, currently, dock widgets inherit fromQWidget
and are "only" children ofQDockWidget
containers, without access to the "parent"QDockWidget
instances.Pitch
Provide a mechanism to subscribe to
QDockWidget
signals from within dock widgets inheriting fromQWidget
. This could be done by passing the correspondingQDockWidget
instance (or a thin wrapper exposing, for encapsulation reasons) as argument to the dock widget constructor, similar to what's being done with theViewer
instance.Alternatives
Implement custom events for dock location changes and anything else
QDockWidget
-related that might come up in the future.Additional context
While the
QDockWidget
instance is returned byWindow.add_dock_widget
, this approach is not applicable in situations where the user opens the dock widget via the GUI menu.The text was updated successfully, but these errors were encountered: