Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QComboBox::setItemData doesn't update data as expected..
Forum Update on Monday, May 27th 2025

QComboBox::setItemData doesn't update data as expected..

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 790 Views
  • 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.
  • M Offline
    M Offline
    MachoX
    wrote on last edited by
    #1

    Here's the code:
    @
    class MyType
    {
    public:
    QString key;
    QString val;
    bool operator==(const MyType& other) const
    {
    return key == other.key;
    }

    bool operator<(const MyType& other) const
    {
        return key < other.key;
    }
    

    };

    Q_DECLARE_METATYPE(MyType)

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    qRegisterMetaType<MyType>("MyType");
        QMetaType::registerComparators<MyType>();
    
    MyType mytype;
    mytype.key = "abc";
    mytype.val = "zxc";
    QComboBox* cmb = new QComboBox();
    cmb->addItem("123", QVariant::fromValue(mytype));
    
    qDebug() << "key:" << cmb->currentData().value<MyType>().key << " val:" << cmb->currentData().value<MyType>().val;
    //OutPut: key: "abc"  val: "zxc"
    
    mytype.val = "vbn";
    cmb->setItemData(cmb->currentIndex(), QVariant::fromValue(mytype));
    qDebug() << "key:" << cmb->currentData().value<MyType>().key << " val:" << cmb->currentData().value<MyType>().val;
    //OutPut: key: "abc"  val: "zxc"       Expected: key: "abc"  val: "vbn"
    
    return app.exec();
    

    }
    @

    Eventually I found out the trick by changing the implementation of the overridden comparator "==" into "{ return (key == other.key && val == other.val);}"
    But that's another way of diverging from my expectation. As the members of MyType are so named, when using QComboBox::findData, as long as MyType::key matches, that's the data I want.
    Is there any workaround about this?

    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