pyQT Combobox print output upon changed

十年热恋 提交于 2019-12-20 04:32:51

问题


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_())

回答1:


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_())


来源:https://stackoverflow.com/questions/48071926/pyqt-combobox-print-output-upon-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!