Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

python - 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_())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...