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. Get the row-Index in a model
Forum Updated to NodeBB v4.3 + New Features

Get the row-Index in a model

Scheduled Pinned Locked Moved General and Desktop
modelqcombobox
2 Posts 1 Posters 3.1k 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.
  • M Offline
    M Offline
    Mr. Kibu
    wrote on last edited by
    #1

    Hi!

    I have got a model (QSqlTableModel) and I have a value that contains my model i.e. in column 2.
    How do I get the index of the row for my value.

    Why do I need this:
    I have two comboboxes. After the selection of a value in the first combobox, I want to set a value (default value that depends on the choice of the first combobox) to the second combobox. In the second combobox I need the index of the row to set the value to the second combobox (with setcurrendIndex()). Or is there a smarter way?

    Thank you!

    Franz

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr. Kibu
      wrote on last edited by
      #2

      I have solved my problem in this way:

      First I have changed the modelColumn to the Column with the key-value in the second combobox:

          ui->cbCostCenterGr->setModelColumn(0);
      

      Then I fetch the key-value for the second combobox from the first combobox...

          QModelIndex id = ui->cbCostCenter->model()->index(index, 2);
          QString _CcGr = id.data().toString();
      

      ...and search the value in the second combobox:

          ui->cbCostCenterGr->setCurrentIndex(ui->cbCostCenterGr->findText(_CcGr));
      

      At least I change the modelColumn of the second combobox to the column with the text (to display the text and not the key-value):

          ui->cbCostCenterGr->setModelColumn(1);
      

      Here is the hole function:

      void VoucherPosForm::on_cbCostCenter_activated(int index)
      {
      
          ui->cbCostCenterGr->setModelColumn(0);
      
          QModelIndex id = ui->cbCostCenter->model()->index(index, 2);//"2" is the key-column
          QString _CcGr = id.data().toString();
      
          ui->cbCostCenterGr->setCurrentIndex(ui->cbCostCenterGr->findText(_CcGr));
      
          ui->cbCostCenterGr->setModelColumn(1);
      }
      

      It works fine, but is there a straighter way??

      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