-1

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_())
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • redundant question, you got the answer already a few days ago here: http://stackoverflow.com/questions/31998023/qcombobox-and-app-setstylecleanlooks – a_manthey_67 Aug 19 '15 at 17:46

2 Answers2

3
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

Achayan
  • 5,720
  • 2
  • 39
  • 61
2

Working solution is posted below. While using item.setData() method we should specify a Role with which we associate the data.

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_())
alphanumeric
  • 17,967
  • 64
  • 244
  • 392