如何获取QComboBox Item数据
How to get QComboBox Item data
而不是使用 .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_())
下面发布了工作解决方案。
在使用 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_())
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_())
我认为这应该适合你
而不是使用 .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_())
下面发布了工作解决方案。
在使用 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_())
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_())
我认为这应该适合你