Instead of using .addItem("Item Name", "My Data")
to populate the QComboBox
I create its item first:
item = QtGui.QStandardItem("Item Name")
Then I set item's data:
item.setData("My data")
Question. How to get the data stored in Combo's Item from inside of currentIndexChanged()
method which gets the clicked ComboBox item's index as an argument:
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_())
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_())
This should work for you i think