如何在QMainWindow中创建的一个QObject中的两个小部件之间进行通信

问题描述 投票:0回答:1

我对Qt中的信号和插槽有疑问。假设我有三个类和下面的方法(忽略GUI和其他内容的创建):

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWin, self).__init__(parent)

        car1 = CarControl(self)
        tireWind = car1.getTireWind()
        self.mdiArea.addSubWindow(tireWind)


class CarControl(QObject):

    def __init__(self, parent=None):
        super(CarControl, self).__init__()

        self.tire = TireWind(parent)
        self.tire.sendEmptyTireSignal.connect(self.printProblem)

    def getTireWind(self):
        return self.tire

    @Slot(str)
    def printProblem(self, probl):
        print(probl)


class TireWind(QWidget):

    empty_tire = Signal(str)

    def __init__(self, parent=None):
        super(Tire, self).__init__(parent)

        self.button_emptyTire.clicked.connect(self.sendEmptyTireSignal)

    def sendEmptyTireSignal(self):
        self.empty_tire.emit("EMPTY TIRE")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

这是代码的简短版本。在MainWindow中,我有mdiArea,正在创建一个对象来控制在Car Windows(CarControl)中发生的一切,然后将其QWidget TireWind添加为mdiArea中的子窗口。问题是,当我在子窗口button_emptyTire中单击TireWind时,它应该发出信号empty_tire,而我的“控制器” CarControl应该识别出它并运行方法printProblem。但是我没有实现。如何在TireWindCarControl之间进行通信?我在CarControl中有多个QWidget,所以我想在CarControl

中管理其所有小部件之间

希望代码足以理解问题:)

python c++ qt pyqt pyside
1个回答
0
投票

您正在使用此行将功能连接到插槽:

self.tire.sendEmptyTireSignal.connect(self.printProblem)

相反,它应该显示为

self.tire.empty_tire.connect(self.printProblem)

因为empty_tire是您要连接插槽的信号。

© www.soinside.com 2019 - 2024. All rights reserved.