简体   繁体   English

获取添加到 QMainWindow 的停靠小部件的参考

[英]Get reference of dock-widget added to QMainWindow

I use a software written using Qt and it has a QMainWindow object.我使用一个用Qt编写的软件,它有一个QMainWindow对象。 I need to get the reference of a widget when added to QMainWindow object already exists.当添加到QMainWindow对象时,我需要获取小部件的引用已经存在。 I have looked at Qt documentation.我看过 Qt 文档。 I hoped there was an event childAdded or something, but I couldn't find anything.我希望有一个事件childAdded什么的,但我找不到任何东西。

Please review this code:请查看此代码:

import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QDockWidget
from PyQt5.QtCore import Qt

class MainWindow(QMainWindow):    
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)    
        self.button = QPushButton("A Button!")
        self.setCentralWidget(self.button)

def do_something(w):
    w.setWindowTitle("Newly Added")

def add_dock_widget(win):
    win.addDockWidget(Qt.RightDockWidgetArea, QDockWidget())

app = QApplication(sys.argv)
window = MainWindow()
window.show()

add_dock_widget(window)
# At this point, somehow, I need to get the reference of added widget.
# Because add_dock_widget method doesn't return any reference.

# I'm looking for something like that, so that I can manipulate the widget:
# window.childAdded[****].connect(do_something)

app.exec_()

The software allows to write python script in it.该软件允许在其中编写python脚本。 When a child is added to GUI, I need to change something within the child using script.当一个孩子被添加到 GUI 时,我需要使用脚本在孩子中改变一些东西。

Depending on your background goal there are several alternatives:根据您的背景目标,有几种选择:

- findChild() or findChildren(): - findChild() 或 findChildren():

If you know the object class you can use findChild or findChildren:如果您知道对象类,您可以使用 findChild 或 findChildren:

if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    add_dock_widget(window)
    dockwidget = window.findChild(QDockWidget)
    do_something(dockwidget)

    # or
    # for dockwidget in window.findChildren(QDockWidget)
    # do_something(dockwidget)

    app.exec_()

But this method is limited since you can have many widgets of the same type and you could not distinguish which object is the one you want so you will have to do some new filter passed in another feature.但是这种方法是有限的,因为您可以拥有许多相同类型的小部件,并且您无法区分哪个对象是您想要的对象,因此您必须执行一些传入另一个功能的新过滤器。

- Override the event method or use an event filter: - 覆盖事件方法或使用事件过滤器:

class MainWindow(QMainWindow):    
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)    
        self.button = QPushButton("A Button!")
        self.setCentralWidget(self.button)

    def event(self, e):
        if e.type() == QEvent.ChildAdded:
            if isinstance(e.child(), QDockWidget):
                do_something(e.child())
        return super(MainWindow, self).event(e)
class ChildAddedFilter(QObject):
    def __init__(self, widget):
        super(ChildAddedFilter, self).__init__(widget)
        self._widget = widget
        self._widget.installEventFilter(self)

    def eventFilter(self, o, e):
        if o is self._widget and e.type() == QEvent.ChildAdded:
            if isinstance(e.child(), QDockWidget):
                do_something(e.child())
        return super(ChildAddedFilter, self).eventFilter(o, e)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = MainWindow()
    f = ChildAddedFilter(window)
    window.show()
    add_dock_widget(window)
    app.exec_()

You can simply return the dock-widget from add_dock_widget , like this:您可以简单地从add_dock_widget返回dock-widget,如下所示:

def add_dock_widget(win):
    dock = QDockWidget(win)
    win.addDockWidget(Qt.RightDockWidgetArea, dock)
    return dock

app = QApplication(sys.argv)
window = MainWindow()
window.show()

dock = add_dock_widget(window)

do_something(dock)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM