【问题标题】:How to get QComboBox Item data如何获取 QComboBox 项目数据
【发布时间】:2015-11-11 21:45:19
【问题描述】:

而不是使用.addItem("Item Name", "My Data") 来填充QComboBox

我先创建它的项目:

item = QtGui.QStandardItem("Item Name")

然后我设置项目的数据:

item.setData("My data")

问题。如何从 currentIndexChanged() 方法内部获取存储在 Combo 的 Item 中的数据,该方法将单击的 ComboBox 项的索引作为参数:

import sys
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui

class MyCombo(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        vLayout=QtGui.QVBoxLayout(self)
        self.setLayout(vLayout)

        self.combo=QtGui.QComboBox(self)
        self.combo.currentIndexChanged.connect(self.currentIndexChanged)
        comboModel=self.combo.model()
        for i in range(3):
            item = QtGui.QStandardItem(str(i))
            item.setData('MY DATA' + str(i) )
            comboModel.appendRow(item)
        vLayout.addWidget(self.combo)

    def currentIndexChanged(self, index):
        print index        

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = MyCombo()
    w.show()
    sys.exit(app.exec_())

【问题讨论】:

标签: python qt pyqt qcombobox


【解决方案1】:
import sys
from PySide import QtGui, QtCore

class MyCombo(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        vLayout=QtGui.QVBoxLayout(self)
        self.setLayout(vLayout)

        self.combo=QtGui.QComboBox(self)
        self.combo.currentIndexChanged.connect(self.currentIndexChanged)
        comboModel=self.combo.model()
        for i in range(3):
            item = QtGui.QStandardItem(str(i))
            comboModel.appendRow(item)
            self.combo.setItemData(i,'MY DATA' + str(i))
        vLayout.addWidget(self.combo)

    def currentIndexChanged(self, index):
        print self.combo.itemData(index)    

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = MyCombo()
    w.show()
    sys.exit(app.exec_())

我认为这应该对你有用

【讨论】:

    【解决方案2】:

    工作解决方案发布在下面。 在使用item.setData() 方法时,我们应该指定一个Role 来关联数据。

    class MyCombo(QtGui.QWidget):
        def __init__(self, *args):
            QtGui.QWidget.__init__(self, *args)
            vLayout=QtGui.QVBoxLayout(self)
            self.setLayout(vLayout)
    
            self.combo=QtGui.QComboBox(self)
            self.combo.currentIndexChanged.connect(self.currentIndexChanged)
            comboModel=self.combo.model()
            for i in range(3):
                item = QtGui.QStandardItem(str(i))
                item.setData('MY DATA' + str(i), QtCore.Qt.UserRole )
                comboModel.appendRow(item)
            vLayout.addWidget(self.combo)
    
        def currentIndexChanged(self, index):     
            modelIndex=self.combo.model().index(index,0)
            print self.combo.model().data(modelIndex, QtCore.Qt.UserRole)
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        w = MyCombo()
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-08
      • 2011-11-20
      • 1970-01-01
      • 2014-06-04
      • 2011-05-19
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      相关资源
      最近更新 更多