user2836797
user2836797

Reputation:

QItemSelection does not change QItemSelectionModel::currentIndex()

I programmatically create a selection in a QTableView:

QItemSelectionModel * selectionModel = ui->tableView->selectionModel();
selectionModel->clear();

// Select the whole row
QModelIndex topLeft = tableModel->index(row, 0, QModelIndex());
QModelIndex topRight = tableModel->index(row, tableModel->columnCount()-1, QModelIndex());
QItemSelection selection(topLeft, topRight);
selectionModel->select(selection, QItemSelectionModel::Select);

Then I have a PushButton, that when clicked, gets the currently selected index:

QModelIndex currentIndex = ui->tableView->selectionModel()->currentIndex();
if (currentIndex.isValid())
{
  // Pop open a dialogue
}

When I make the selection with the first block of code, the row in the table is highlighted. However, when I click the PushButton the dialogue does not open, suggesting that the QModelIndex returned by currentIndex() is invalid. Why is this?

I can get the dialogue to pop up by first clicking with the mouse on the already highlighted table row. Then the current index is valid.

Upvotes: 0

Views: 1425

Answers (1)

vahancho
vahancho

Reputation: 21220

The current index is not the same as a selected index. Do not mix these two concepts. There can be only one current index, however multiple selected indexes at a time. You may check for the selected index instead:

QModelIndexList indexes = ui->tableView->selectionModel()->selectedIndexes();
if (indexes.size() > 0) {
  // Pop open a dialogue
}

Upvotes: 1

Related Questions