Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate user interaction from programmical change: PyQt, QComboBox

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?

like image 956
Ekaterina Mishina Avatar asked Dec 28 '22 06:12

Ekaterina Mishina


1 Answers

There are a few different things you can try.

Firstly, you can make sure you do all your initialization before you connect up the signals.

Secondly, you could use the activated signal, which is only sent whenever the user selects an item. (But note that, unlike currentIndexChanged, this signal is sent even if the index hasn't changed).

Thirdly, you could use blockSignals to temporarily stop any signals being sent while the current index is being changed programmatically.

Here's a script that demonstrates these possibilities:

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_())
like image 169
ekhumoro Avatar answered Dec 31 '22 12:12

ekhumoro