Sta*_*ons 4 python qt pyqt pyqt4 python-3.x
我只是在学习 pyqt,我正在尝试理解标准按钮。我只是在学习,所以如果我做错了什么,请告诉我。
我在 QT Designer 中创建了一个带有一些标准按钮的简单 UI。
我注意到accepted() 和rejected() 信号连接到accept 和reject 槽,所以我写了它们。Ok 按钮和取消按钮按预期工作,但 Apply 按钮根本没有反应。如何将应用按钮连接到插槽?
sample.py - 这是我的示例应用代码:
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_())
Run Code Online (Sandbox Code Playgroud)
Designer.py - 这是自动生成的 QT Designer 代码:
# -*- 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))
Run Code Online (Sandbox Code Playgroud)
您不需要为accept()and编写槽reject(),因为QDialog类已经有了它们。
当您创建一个新表单并选择“带按钮的对话框”时,它将添加一个带有“确定”和“取消”按钮的按钮框,并且accepted()/rejected()信号将自动连接到对话框的现有accept()和reject()插槽。
但请注意,按钮和信号之间没有一对一的关系。相反,有一组按钮角色,每个标准按钮都被分配了一个这些角色。与所有按钮AcceptRole会发出accepted()信号,那些与RejectRole将发射的rejected()信号,而那些与HelpRole将发射的helpRequested()信号。但是其他角色(例如ApplyRole),不发出除 之外的任何特定信号clicked()。
要处理所有其他按钮,您可以像这样添加一个插槽:
class SampleApp(QtGui.QWidget):
def __init__(self, parent=None):
super(SampleApp, self).__init__(parent)
self.ui = designer.Ui_Dialog()
self.ui.setupUi(self)
self.ui.buttonBox.clicked.connect(self.handleButtonClick)
def handleButtonClick(self, button):
sb = self.buttonBox.standardButton(button)
if sb == QtGui.QDialogButtonBox.Apply:
print('Apply Clicked')
elif sb == QtGui.QDialogButtonBox.Reset:
print('Reset Clicked')
# and so on...
Run Code Online (Sandbox Code Playgroud)
您需要在小部件中手动连接clicked来自“应用”按钮的信号。
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)
Run Code Online (Sandbox Code Playgroud)