I'm just learning pyqt and I'm trying to understand Standard buttons. I'm just learning, so let me know if I did something grossly wrong.
I've created a simple UI with some standard buttons in QT Designer.
I noted that the accepted() and rejected() signals are being connected to the accept and reject slots, so I wrote them. The Ok button and cancel button work as expected, but the Apply button simply doesn't react. How do I connect the apply button to a slot?
sample.py - Here's my sample app code:
import sys
from PyQt4 import QtGui
import designer
class SampleApp(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui = designer.Ui_Dialog()
self.ui.setupUi(self)
def reject(w):
print("reject", w)
w.close()
def accept(w):
print("accept", w)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = SampleApp()
myapp.show()
sys.exit(app.exec_())
designer.py - Here's the auto-generated QT Designer code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'designer.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
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(554, 399)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(190, 340, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Apply|QtGui.QDialogButtonBox.Close|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setCenterButtons(False)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
You'll need to connect the clicked
signal from the Apply button manually in your widget.
class SampleApp(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui = designer.Ui_Dialog()
self.ui.setupUi(self)
btn = self.ui.buttonBox.button(QtGui.QDialogButtonBox.Apply)
btn.clicked.connect(self.accept)