要了解行为,我们将使用以下示例:
范例1 :
from PyQt4 import QtCore, QtGuidef on_clicked(): print("clicked")if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) button = QtGui.QPushButton("Press me") for _ in range(4): button.clicked.connect(on_clicked) button.show() sys.exit(app.exec_())
当您按下按钮时,它将打印4次,因为Qt不会记住信号和函数之间是否已经存在连接,因此,如果同一信号和函数之间存在n个连接,则在发出信号时它们将被调用n次。 。
范例2 :
from PyQt4 import QtCore, QtGuidef on_currentIndexChanged(ix): print("currentIndex:", ix)if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) combo = QtGui.QComboBox() print("currentIndex:", combo.currentIndex()) combo.currentIndexChanged.connect(on_currentIndexChanged) button_add = QtGui.QPushButton("Add Items") button_add.clicked.connect(lambda: combo.addItems("1 2 3".split())) button_clear = QtGui.QPushButton("Clear") button_clear.clicked.connect(combo.clear) w = QtGui.QWidget() lay = QtGui.QVBoxLayout(w) for widget in (combo, button_add, button_clear): lay.addWidget(widget) w.show() sys.exit(app.exec_())
QComboBox的默认currentIndex为-1,当您添加项目时,该值将变为currentIndex为0,而当您清洁QComboBox时,currentIndex将恢复为-1。因此,当您添加或清洁项目时,会发出currentIndexChanged信号。
基于以上内容,我将解释您指出的行为:在QCombobox
1中选择新的currentIndex时,将调用测试方法,并且在清理并添加了第一个连接的项目后,如果该方法进入 else
语句,则现在重复相同的事情,因为有一个连接,currentIndexChanged信号被发射两次(一个用于clear(),另一个用于addItems()),并且您还创建了第二个连接,如果再次重复,则发射信号4次(1个clear()x
2个连接+ 1个addItems()x 2个连接)。
解:
- 在仅调用一次的函数中建立连接。
- 为了使clear()信号和addItems()不会发出currentIndexChanged信号,您必须使用blockSignals()。
另外,建议不要修改Qt Designer生成的代码,除使用修饰@ QtCore.pyqtSlot()外,建议创建另一个使用初始类作为接口的类。
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return stry:
_encoding = QtGui.QApplication.UnipreUTF8
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8(“MainWindow”))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8(“centralwidget”))
self.comboBox1 = QtGui.QComboBox(self.centralwidget)
self.comboBox1.setGeometry(QtCore.QRect(310, 150, 171, 31))
self.comboBox1.setObjectName(_fromUtf8(“comboBox1”))
self.comboBox1.addItem(_fromUtf8(“”))
self.comboBox1.addItem(_fromUtf8(“”))
self.comboBox1.addItem(_fromUtf8(“”))
self.comboBox1.addItem(_fromUtf8(“”))
self.comboBox_2 = QtGui.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(310, 240, 171, 41))
self.comboBox_2.setObjectName(_fromUtf8(“comboBox_2”))
self.comboBox_2.addItem(_fromUtf8(“”))
self.comboBox_2.addItem(_fromUtf8(“”))
self.comboBox_2.addItem(_fromUtf8(“”))
self.comboBox_2.addItem(_fromUtf8(“”))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8(“menubar”))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8(“statusbar”))
MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow) QtCore.QmetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) self.comboBox1.setItemText(0, _translate("MainWindow", "select", None)) self.comboBox1.setItemText(1, _translate("MainWindow", "a", None)) self.comboBox1.setItemText(2, _translate("MainWindow", "b", None)) self.comboBox1.setItemText(3, _translate("MainWindow", "c", None)) self.comboBox_2.setItemText(0, _translate("MainWindow", "select", None)) self.comboBox_2.setItemText(1, _translate("MainWindow", "p", None)) self.comboBox_2.setItemText(2, _translate("MainWindow", "q", None)) self.comboBox_2.setItemText(3, _translate("MainWindow", "r", None))
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def init(self, parent=None):
super(MainWindow, self).init(parent)
self.setupUi(self)self.comboBox_2.setEnabled(False) self.comboBox1.currentIndexChanged[str].connect(self.test) self.comboBox_2.currentIndexChanged.connect(self.test1)@QtCore.pyqtSlot(str)def test(self, s): res=['aa','bb','cc','dd'] if s == "- - select - -": self.comboBox_2.setEnabled(False) self.comboBox_2.setCurrentIndex(0) elif not s: self.comboBox_2.setEnabled(True) self.comboBox_2.blockSignals(True) self.comboBox_2.clear() self.comboBox_2.addItem("- - select - -") self.comboBox_2.addItem("New Checklist") self.comboBox_2.blockSignals(False) else: self.comboBox_2.setEnabled(True) self.comboBox_2.blockSignals(True) self.comboBox_2.clear() self.comboBox_2.addItem("- - select - -") self.comboBox_2.addItem("New Checklist") self.comboBox_2.addItems(res) self.comboBox_2.blockSignals(False)@QtCore.pyqtSlot(int)def test1(self, ix): print("Hello", ix)
if name == “main”:
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)