Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Unable to use QComboBox's setItemData() method for QStandardItem objects (PySide)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Language Bindings
2 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tristam
    wrote on last edited by
    #1

    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?)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thebeast44
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved