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 with Data and DisplayValues
Qt 6.11 is out! See what's new in the release blog

QComboBox with Data and DisplayValues

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.9k Views 2 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.
  • Andy314A Offline
    Andy314A Offline
    Andy314
    wrote on last edited by Andy314
    #1

    I want show a simple Combox with displaytext but with internal valuedata which I can set and read. The documentation says that one should use the Qt:UserRole for the valuedata.
    So I have checked this simple code:

        // ui->Voyage is a ComboxBox fillled with a QSqlQueryModel, 
        ui->Voyage->setItemData(0, 1, Qt::UserRole);
        int k=ui->Voyage->itemData(0, Qt::UserRole).toInt();   //  k==0   !
    

    But it does not work. k==0.
    Whats the trick.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Maybe a silly question but does the combo box hold something when you call that code ?

      try with:

      ui->Voyage->addItem(tr("TestMe"));
      ui->Voyage->setItemData(0, 1, Qt::UserRole);
      int k=ui->Voyage->itemData(0, Qt::UserRole).toInt();
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Andy314A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Maybe a silly question but does the combo box hold something when you call that code ?

        try with:

        ui->Voyage->addItem(tr("TestMe"));
        ui->Voyage->setItemData(0, 1, Qt::UserRole);
        int k=ui->Voyage->itemData(0, Qt::UserRole).toInt();
        
        Andy314A Offline
        Andy314A Offline
        Andy314
        wrote on last edited by Andy314
        #3

        @SGaist
        It holds something: a QSqlQueryModel by setModel().
        I have thought about the mechnismn. I think the combination of "dynamic" database data for the display-text and fixed data for the UserRole can not work !? Respectively I should put the UserData in the model.

        Therefore I use the data in the model directy now and fill the
        Combox (addItem(DisplayText, ID) by code. So it work.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Either that or create a subclass of QSqlQueryModel that handles the UserRole

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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