Get the row-Index in a model
-
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
-
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??