1

I am facing a problem with a QListView component.

I created a simple form with a listview and a tableview. Then I put this code, both widgets populate with the data model as I want:

QSqlQueryModel * modela = new QSqlQueryModel();
QSqlQueryModel * modelb = new QSqlQueryModel();

[...]

ui->listView->setModel(modela);
ui->tableView->setModel(modelb);

[...]

void MyWindow::on_listView_clicked(const QModelIndex &index)
{
 ui->tableView->setCurrentIndex(ui->listView->currentIndex());
}

void MyWindow::on_tableView_clicked(const QModelIndex &index)
{
 ui->listView->setCurrentIndex(ui->tableView->currentIndex()); 
 // FAILS, does not react...  
}

The first slot (when I click any item in the listview widget) works as expected, it automatically selects the corresponding item in the tableview widget, but the second case does not work, it just does not select any item in the listview...

What I want is that whatever item the user clicks in the tableview gets selected in the listview.

Is it possible? I tried hard, looking for examples and the official qt documentation, but I don't find the right way to do (also tried to connect with signal/slots, but I don't know how to exactly connect both widgets).

Thanks in advance.

  • 1
    How do you know what to select? You use table view indexes to select the items in the list view? Hm... – vahancho Sep 16 '14 at 12:46
  • The index are the same for both widgets, so if the user clicks on the third row on the tableview, then the third row of the listview must be selected (or highlighted). I am currently trying this solution: void MyWindow::on_tableView_clicked(const QModelIndex &index) { ui->listView->setCurrentIndex(ui->tableView->currentIndex()); ui->listView->selectionModel()->select(ui->listView->model()->index(ui->tableView->currentIndex().row(),0),QItemSelectionModel::SelectCurrent); And it works, but It seems very ugly way to do... } – Lando Calc_rissian Sep 16 '14 at 12:50
  • Why not using two table views or two list views? – vahancho Sep 16 '14 at 12:53
  • @lando-calc-rissian You are on the right way, see my answer – Ezee Sep 16 '14 at 12:59

1 Answers1

3

QModelIndex is an integral part of a certain QAbstractItemModel. It means that you can't use an index from model A to select an item in a view of model B.

QModelIndex is not just a couple of x,y. It also keeps a pointer to a model which created it.

So if you need to select the same row as selected in the first view, you need to extract a row from the first index, then get a right index in the second model and use it to select an item in the second view:

void selectTheSameRow(const QModelIndex& indexFromModelA)
{
  int row = indexFromModelA.row();
  QModelIndex indexFromModelB = modelB->index(row, 0);
  viewB->setCurrentIndex(indexFromModelB);
}
Ezee
  • 4,214
  • 1
  • 14
  • 29
  • Calling `setCurrentIndex()` doesn't mean selecting the item. – vahancho Sep 16 '14 at 13:07
  • It depends on `selectionMode` but in most cases it does. Look at `QItemSelectionModel::setCurrentIndex` (model calls this method) it forwards to `select`. – Ezee Sep 16 '14 at 13:13
  • @Ezee you answer clarifies very well my misunderstanding about QModelIndex, I wonder why there are still some widgets (like combobox) where QModelIndex appears to be just an integer, but this is another topic... Thanks for your answer ! – Lando Calc_rissian Sep 17 '14 at 06:12