floflo29
floflo29

Reputation: 2321

Communicate between two MainWindow() in Qt

My app, in Qt, consists in 2 different windows (but both inherited from QtGui.QMainWIndow), and I am wondering how to communicate between them. Moreover, does using multiple QMainWindow is generally a good approach?

Upvotes: 0

Views: 622

Answers (1)

user3419537
user3419537

Reputation: 5000

Connect signals and slots between the two window classes when you instantiate them.

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    window1 = MyMainWindow1()
    window1.show()

    window2 = MyMainWindow2()
    window2.show()

    # connect signals to communicate between windows
    window1.someSignal.connect(window2.someSlot)
    window2.anotherSignal.connect(window1.anotherSlot)

    app.exec()

QMainWindow is designed to be used as the main application window; it simplifies the addition of common window features like toolbars and menus. However, I don't think there is any harm in having multiple instances.

You can also just use anyQWidget:

window = QtWidgets.QWidget()    # note that no parent is given
window.show()

Upvotes: 2

Related Questions