PDA

View Full Version : How to connect two tableView widgets



kode
22nd December 2010, 13:13
Hi all,

I have main window with two tableView widgets. The data has primary key and foreign key and I want to connect it using onclick slot. The idea is to update the second table when click on the first and modify where part in the query.

The problem is that I can get the index of the first table but i need the value. Here is the code of the slot:


void SBTable::on_tableView_clicked(QModelIndex index)
{
QString num;

num = this->db->model->data(this->db->model->index(index.row(), 0)).toString();

this->SBMessageBox("idx: " + num + ".");
}

Thanks in advance!

Added after 49 minutes:

I found a solution:


num = this->ui->tableView->model()->data(
this->ui->tableView->model()->index(index.row() ,
0)).toString();

One other problem appear now:

How to set the model to the second tableView?

ChrisW67
22nd December 2010, 23:29
The index passed in to the slot encodes the source model. Also, the QModelIndex::sibling() method can be useful.


num = this->ui->tableView->model()->data(
this->ui->tableView->model()->index(index.row() ,
0)).toString();

becomes something like:

num = index.sibling(index.row(), 0).data().toString();

How you adjust the second view's content is entirely dependent on how you have constructed the model and view. If it is a simple string filter then I'd be tempted to use the base model through a QSortFilterProxyModel. Then just use the QSortFilterProxyModel::setFilterFixedString() slot.