QTableView: select row with a specific id
-
After sorting (i.e.
ui->table->sortByColumn(MODEL_PACKAGES_COL_NAME)
) the order of the rows in theQTableView
may be different than those in the model. If I need to select (highlight) a row with a specific id, I do this:void DialogPackages::selectId(int id) { for (int i = 0; i < _model->rowCount(); i++) { QModelIndex index = _model->index(i, MODEL_PACKAGES_COL_ID); if (id == _model->data(index)) ui->table->selectRow(i); } }
it works, but I wonder if there's something better to avoid to cycle all the rows every time.
-
I don't see another way, apart from that you should break out once the row is found.
-
@Christian-Ehrlicher said in QTableView: select row with a specific id:
I don't see another way, apart from that you should break out once the row is found.
Yep, of course I forgot to break the loop.
In other words, there are no efficient find methods built-in. -
@Mark81 said in QTableView: select row with a specific id:
there are no efficient find methods built-in.
build-in - no since it's your data, not something Qt has control of it.
-
@Christian-Ehrlicher said in QTableView: select row with a specific id:
build-in - no since it's your data, not something Qt has control of it.
Not actually, I thought about a generic method like this:
int QTableView::findRow(int col, QVariant data);
it would return the (first) row number where
data
is present into columncol
. -1 otherwise.
Or a more generic one:QList<int> QTableView::findRows(int col, QVariant data);
of course it's trivial to implement them in a rough way like I did above.
I was just afraid I didn't search well the documentation. -
Maybe QAbstractItemModel::match() - but custom stuff is faster I would guess