简体   繁体   English

更改后的pyQT Combobox打印输出

[英]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. 我想基于哪个combox和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". 当我在左侧组合框中按下“第二”时,我想要输出“ 0,2”,而当我在右侧组合框中按下“第二”时,我想要输出“ 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. 这是没有必要使用lambda函数来发送额外的信息,如果QComboBox被使用, QComboBox可以存储信息为每个索引, addItem()具有其中能够保存的信息的附加参数,我们可以通过访问它itemData()的方法,我们可以使用setItemData()方法添加其他信息。

To know that QComboBox emitted the signal we can use sender() , this method returns the object that emits the signal. 要知道QComboBox发出了信号,我们可以使用sender() ,此方法返回发出信号的对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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