简体   繁体   中英

pyQT Combobox print output upon changed

I want to create an event (print in this case) based on which combox and which row in the combox. I had a look on this old post and made some extension. Does it make some sense? When I press "second" in the left combox I want the output "0, 2" and when I press the "second" in the right combox I want the output "1, 2".

from PyQt4 import QtCore, QtGui
import sys


class MyClass(object):
    def __init__(self, arg):
        super(MyClass, self).__init__()
        self.row = arg
        self.col = []

    def add_column(self, col):
        self.col.append(col)


class myWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)
        comboBox = [None, None]
        myObject = [None, None]
        slotLambda = [None, None]
        for j in range(2):
            comboBox[j] = QtGui.QComboBox(self)
            if j > 0:
                comboBox[j].move(100, 0)
            test = [['first', 1], ['second', 2]]
            myObject[j] = MyClass(j)
            for num, value in test:
                comboBox[j].addItem(num)
                myObject[j].add_column(value)
                slotLambda[j] = lambda: self.indexChanged_lambda(myObject[j])
            comboBox[j].currentIndexChanged.connect(slotLambda[j])

    @QtCore.pyqtSlot(str)
    def indexChanged_lambda(self, string):
        print string.row, string.col

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myApp')
    dialog = myWindow()
    dialog.show()
    sys.exit(app.exec_())

It is not necessary to use lambda functions to send additional information if QComboBox is used, QComboBox can store information for each index, addItem() has an additional parameter where information can be saved and we can access it through the itemData() method, we can add another information with the setItemData() method.

To know that QComboBox emitted the signal we can use sender() , this method returns the object that emits the signal.

All of the above is implemented in the following example:

from PyQt4 import QtCore, QtGui
import sys

class myWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        lay = QtGui.QHBoxLayout(self)
        test = [['first', 1], ['second', 2]]

        for j in range(5):
            comboBox = QtGui.QComboBox(self)
            lay.addWidget(comboBox)
            for i, values in enumerate(test):
                text, data = values
                comboBox.addItem(text, (j, data))
            comboBox.currentIndexChanged.connect(self.onCurrentIndexChanged)

    @QtCore.pyqtSlot(int)
    def onCurrentIndexChanged(self, ix):
        combo = self.sender()
        row, column = combo.itemData(ix)
        print(row, column)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myApp')
    dialog = myWindow()
    dialog.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM