QDialog does not open from the main window (pyQt)

I try to start a dialog by clicking a button in the main window: Here is the (generated qtdesigner) code that I modified only to test it. I set the showDial function to show the dial when the button is clicked. But this does not work:

from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: _fromUtf8 = lambda s: s class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName(_fromUtf8("Dialog")) Dialog.setWindowModality(QtCore.Qt.WindowModal) Dialog.resize(400, 300) Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8)) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): pass class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(309, 148) MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.pushButton = QtGui.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(50, 30, 191, 71)) self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Dialog", None, QtGui.QApplication.UnicodeUTF8)) self.pushButton.setObjectName(_fromUtf8("pushButton")) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked(QAbstractButton*)")), self.showDial) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): pass def showDial(self): Dialog = QtGui.QDialog() u = Ui_Dialog() u.setupUi(Dialog) Dialog.exec_() if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) MainWindow = QtGui.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_()) 
+4
source share
1 answer

There is an error in the signal connection, it should be:

 QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.showDial) 

or in a more pythonic New signal and slot syntax for PyQt 4.5+:

 self.pushButton.clicked.connect(self.showDial) 
+3
source

Source: https://habr.com/ru/post/1393482/


All Articles