Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set current index in QCombobox by the value of it's model column



  • Hi, dear developers.
    I have a QCombobox with attached QSqlTableModel. I need to set up a current index by the primary key of the model, which is saved in another table as foreign key.
    May be something like this:

    int index = myComboBox->findData(...);
    myComboBox->setCurrentIndex(index);
    

    How can I do this properly? Please give some code example.
    Thanks in advance,
    Best wishes



  • @Valentine

    I need to set up a current index by the primary key of the model, which is saved in another table as foreign key.

    QComboBox::setCurrentIndex() selects by item number, from 0 to number of items in combo box (- 1). In your situation, how does it make sense that this is some primary key value (and taken from some other table at that)? I think I am misunderstanding or you are mis-expressing what you want?

    The example code you show will find some existing item in the combo and then select that item. Which is fine in itself, but apparently not what you have in mind?



  • Can you use QSqlRelationalTableModel instead?


Log in to reply