I am starting to learn Python and PyQt. I am currently struggling with a very simple problem of connecting signals and slots, with Dialog forms formed in the QDesigner form. I want to connect the QDialog button. The code does not generate an error. A dialog is shown as expected. But when you click on the buttons, nothing happens.
As an alternative, I tried to include the code form Ui_Dialogdirectly in my target class Testdialog. Then the connection worked. It looks like I made a mistake in inheriting properties from Ui_Dialogto Testdialogand / or in how I want to execute the dialog.
My main program is as follows:
from __future__ import unicode_literals
import sys
from PyQt4 import *
from PyQt4 import QtGui
from PyQt4.QtCore import SIGNAL, QObject
import UI_Test
class Testdialog(QtGui.QDialog, UI_Test.Ui_Dialog):
def __init__(self,parent=None):
super(Testdialog, self).__init__(parent)
self.setupUi(self)
print("Connect buttons")
self.connect(self.pushButton_Ok, SIGNAL("clicked()"), self.clickedOk)
self.connect(self.pushButton_Cancel, SIGNAL("clicked()"), self.clickedCancel)
def clickedCancel(self):
print ("Cancel")
def clickedOk(self):
print ("Ok")
if True:
qApp = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
u = Testdialog()
u.setupUi(Dialog)
Dialog.exec_()
sys.exit(qApp.exec_())
When I click on the bushbuttons buttons, nothing happens. It seems that the connection is not working.
? ? ?
UI_Test.py - , QtDesigner pyuic. ( ).
, :
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(271, 70)
self.pushButton_Ok = QtGui.QPushButton(Dialog)
self.pushButton_Ok.setGeometry(QtCore.QRect(20, 20, 93, 28))
self.pushButton_Ok.setObjectName(_fromUtf8("pushButton_Ok"))
self.pushButton_Cancel = QtGui.QPushButton(Dialog)
self.pushButton_Cancel.setGeometry(QtCore.QRect(130, 20, 93, 28))
self.pushButton_Cancel.setObjectName(_fromUtf8("pushButton_Cancel"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton_Ok.setText(_translate("Dialog", "OK", None))
self.pushButton_Cancel.setText(_translate("Dialog", "Cancel", None))