5

I have several QComboBoxes in my PyQt4/Python3 GUI and they are filled with some entries from a database during the initialisation. Initial CurrentIndex is set to 0. There is also a tick box which changes the language of the items in my combo boxes. To preserve current user selection I backup index of the current item and setCurrentIndex to this number after I fill in ComboBox with translated items. All those actions emit currentIndexChanged signal.

Based on the items selected in QComboBoxes some plot is displayed. The idea is to redraw the plot online - as soon as the user changes any of ComboBox current item. And here I have a problem since if I redraw the plot every time signal currentIndexChanged is emited, I redraw it also several times during initialization and if the translation tick box selection was changed.

What is the best way to separate these cases? In principle I need to separate programmical current Index Change from the user, and update the plot only in the later case (during GUI initialisation I can programically call update plot function once). Should I write/rewrite any signal? If so, I never did that before and would welcome any hint or a good example. Use another signal? Or maybe there is a way to temporary block all signals?

4

1 回答 1

11

您可以尝试几种不同的方法。

首先,您可以确保在连接信号之前完成所有初始化。

其次,您可以使用激活信号,该信号仅在用户选择项目时发送。(但请注意,与currentIndexChanged不同,即使索引未更改,也会发送此信号)。

第三,您可以使用blockSignals在以编程方式更改当前索引时暂时停止发送的任何信号。

这是一个演示这些可能性的脚本:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QVBoxLayout(self)
        self.combo = QtGui.QComboBox()
        self.combo.setEditable(True)
        self.combo.addItems('One Two Three Four Five'.split())
        self.buttonOne = QtGui.QPushButton('Change (Default)', self)
        self.buttonOne.clicked.connect(self.handleButtonOne)
        self.buttonTwo = QtGui.QPushButton('Change (Blocked)', self)
        self.buttonTwo.clicked.connect(self.handleButtonTwo)
        layout.addWidget(self.combo)
        layout.addWidget(self.buttonOne)
        layout.addWidget(self.buttonTwo)
        self.changeIndex()
        self.combo.activated['QString'].connect(self.handleActivated)
        self.combo.currentIndexChanged['QString'].connect(self.handleChanged)
        self.changeIndex()

    def handleButtonOne(self):
        self.changeIndex()

    def handleButtonTwo(self):
        self.combo.blockSignals(True)
        self.changeIndex()
        self.combo.blockSignals(False)

    def changeIndex(self):
        index = self.combo.currentIndex()
        if index < self.combo.count() - 1:
            self.combo.setCurrentIndex(index + 1)
        else:
            self.combo.setCurrentIndex(0)

    def handleActivated(self, text):
        print('handleActivated: %s' % text)

    def handleChanged(self, text):
        print('handleChanged: %s' % text)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
于 2012-02-08T19:40:35.777 回答