QComboBox with Data and DisplayValues



  • 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.


  • Lifetime Qt Champion

    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();
    


  • @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.


  • Lifetime Qt Champion

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


Log in to reply
 

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