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. How to set current index in QCombobox by the value of it's model column
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.2k 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.
  • V Offline
    V Offline
    Valentine
    wrote on last edited by Valentine
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • V Valentine

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

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

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Can you use QSqlRelationalTableModel instead?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2

        • Login

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