Unable to use QComboBox's setItemData() method for QStandardItem objects (PySide)



  • I've checked everywhere to see whether someone else has reported this as a bug, but I've seen nothing--as a result, I'm not sure if I'm doing something wrong, but I don't believe so because this is extremely straightforward. Whenever I set the data value of a QComboBox item (using setItemData() method OR embedding the data in the addItem() method as in the code below), it apparently doesn't take -- even though no error is reported, the call to retrieve the data (using the itemData() method) returns a NoneType! I quickly created a very simple QDialog() script to print the results to the terminal; please let me know if you reproduce the problem or not:

    @from PySide.QtCore import *
    from PySide.QtGui import *
    import sys

    class Form(QDialog):
    def init(self, parent=None):
    super(Form, self).init(parent)
    self.setWindowTitle("Adding QStandardItem as the value to QComboBox's setData() method")

    self.model = QStandardItemModel()
    item1 = QStandardItem("Item 1")
    item2 = QStandardItem("Item 2")
    item3 = QStandardItem("Item 3")
    self.model.appendRow(item1)
    self.model.appendRow(item2)
    self.model.appendRow(item3)

    option1 = "option 1"
    option2 = "option 2"
    option3 = "option 3"

    self.comboBox = QComboBox()
    self.comboBox.addItem(option1, item1)
    self.comboBox.addItem(option2, item2)
    self.comboBox.addItem(option3, item3)

    layout = QVBoxLayout()
    layout.addWidget(self.comboBox)
    self.setLayout(layout)

    self.comboBox.currentIndexChanged.connect(self.printItemData)

    def printItemData(self):
    print "The item associated with that index is: ", self.model.item(self.comboBox.currentIndex())
    print "The data linked to that item is: ", self.comboBox.itemData(self.comboBox.currentIndex())

    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()@

    I was under the impression that the value of setItemData() could be any object. Is this a bug? (Or do I perhaps have a bad installation?)



  • I'm looking at C++ Reference, so I may be wrong about this but in Qt C++ addItem / setItemData declaration shows "QVariant". maybe you might want to use row index or "Item n" for the ItemData


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.