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 in a QCombobox the current item with a non viewed column of my model
Forum Updated to NodeBB v4.3 + New Features

How to set in a QCombobox the current item with a non viewed column of my model

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 5.8k Views 1 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.
  • ON7AMI - Jean Paul MertensO Offline
    ON7AMI - Jean Paul MertensO Offline
    ON7AMI - Jean Paul Mertens
    wrote on last edited by
    #1

    Hello,

    I'm used in C# .Net to set a combobox current row with or an .item, or an .index or a .value.
    I find out that in Qt a QCombobox has a .currentIndex corresponding with the .index, the currentText is the .value but I don't find a way to set the .item.

    What I want to do is:

    • I have a combobox populated with a QTableModel with in each record a field "ID" and a field "Description"
    • I set my .setModelColumn(1); to make my "Description" the viewed data
    • now I want to set my current selected item in the combobox by the "ID" field of my model.

    Is there a way t do so?

    tnx

    1 Reply Last reply
    1
    • J Offline
      J Offline
      Jeroen3
      wrote on last edited by
      #2

      Use an indexOf approach on your model and use setCurrentIndex(int index) on the result of that?

      1 Reply Last reply
      0
      • ON7AMI - Jean Paul MertensO Offline
        ON7AMI - Jean Paul MertensO Offline
        ON7AMI - Jean Paul Mertens
        wrote on last edited by ON7AMI - Jean Paul Mertens
        #3

        @Jeroen3

        That's my problem, wog can I find this index.

        I have a query result of

           ID   !   Description
        --------+------------------------------
             1  !  Jef
             2  ! Dave
             3  ! Harry
             5  ! Mary
             8  ! John
          ...   !   ...
        

        this is stored in my QSqlQueryModel

        so how can I find and set my combobox pointing at) the row with ID=3 while displaying column 1 (the Description)

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jeroen3
          wrote on last edited by
          #4

          Maybe with QSqlTableModel::setFilter?

          1 Reply Last reply
          0
          • ON7AMI - Jean Paul MertensO Offline
            ON7AMI - Jean Paul MertensO Offline
            ON7AMI - Jean Paul Mertens
            wrote on last edited by
            #5

            Perhaps I don't explain it correctly. What I want to do is that the user can select in the whole table of descriptions, so the whole table has to be in the model.

            Instead of setting

             QComboBox::setItemText("Harry") 
            

            I want to use something like

             QComboBox::setItemUsingMyColumnZeroOfTheModel(4);
            

            for the moment I do it this way:

                int val = 507;
                ui->cbxGemeente->setModel(Gemeenten_model);
                ui->cbxGemeente->setModelColumn(0);
                int indx = ui->cbxGemeente->findText( QString::number(val));
                ui->cbxGemeente->setModelColumn(1);
                ui->cbxGemeente->setCurrentIndex(indx);
            

            As always the way from Brussels to Paris passing by Rome is a nice trip but much to long...

            So I think there must be a more efficient way to attain the same result.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jeroen3
              wrote on last edited by Jeroen3
              #6

              Why can't you have your model spit out the relation between ID and Description?
              Since apparantly ID is not consistent with QComboBox::index.
              QString Gemeenten_model->getDescription(id)?

              // Returns index in table "WHERE 'ID'==id".
              int Gemeenten_model->getTableIndex(id)?
              
              ui->cbxGemeente->setCurrentIndex( model->getTableIndex(id) );
              

              Another option is to inherit QComboBox and expand the functionality of that.

              ON7AMI - Jean Paul MertensO 1 Reply Last reply
              1
              • J Jeroen3

                Why can't you have your model spit out the relation between ID and Description?
                Since apparantly ID is not consistent with QComboBox::index.
                QString Gemeenten_model->getDescription(id)?

                // Returns index in table "WHERE 'ID'==id".
                int Gemeenten_model->getTableIndex(id)?
                
                ui->cbxGemeente->setCurrentIndex( model->getTableIndex(id) );
                

                Another option is to inherit QComboBox and expand the functionality of that.

                ON7AMI - Jean Paul MertensO Offline
                ON7AMI - Jean Paul MertensO Offline
                ON7AMI - Jean Paul Mertens
                wrote on last edited by
                #7

                @Jeroen3

                The value in my ID column of the model is not synchronized with the table-index (deleted records make that the sequence is broken.). and the value in the ID column is the only thing I have.

                J 1 Reply Last reply
                0
                • ON7AMI - Jean Paul MertensO ON7AMI - Jean Paul Mertens

                  @Jeroen3

                  The value in my ID column of the model is not synchronized with the table-index (deleted records make that the sequence is broken.). and the value in the ID column is the only thing I have.

                  J Offline
                  J Offline
                  Jeroen3
                  wrote on last edited by Jeroen3
                  #8

                  Yes. I noticed that.
                  That's why I suggested to create a function that converts "ID" to the TableIndex of which you have column 1 attached to the QComboBox.
                  Basically you just search for that "ID" in your model, and return the table index where is is found. For example:

                     ID   !   Description
                  --------+------------------------------
                       1  !  Jef
                       2  ! Dave
                       3  ! Harry
                       5  ! Mary
                       8  ! John
                  

                  model->getTableIndex( 5 ) == 3
                  If you're into the mood of doing things properly you might even return a QModelIndex.

                  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