View Full Version : using QSqlRelationalTableModel opposite of the example

5th June 2010, 03:57
I am new to using QSql classes, been a few years since I have done any Qt programming, actually. My model:

Table: PriceList
Fields: PriceListId, Name

1 ...* to

Table: ProductItem
Fields: ProductItemId, PriceListId, ...

The ProductItem table has a foreign key (PriceListId) to the PriceList table.

I have a ComboBox who's model is the PriceList model. There is a TableView under it, when a different PriceList is selected, I want the TableView to update. How do I wire up the QSqlRelationalTableModel?


5th June 2010, 08:40
Have you considered attaching the QTableView to a QSortFilterProxyModel model (overlaid on your product item model) and using the QComboBox::currentIndexChanged() signal to change the filtering on the proxy model for the foreign key column?

8th June 2010, 03:45
Sorry it took me a few days to get back to you, been swamped...

I just read over QSortFilterProxyModel model and it isn't looking like the solution. I just realized what I am really looking for, an example of how to implement Master/Detail with the Model View. The PriceList is the master, in a comboBox, the ProductList is the details in the TableView.


8th June 2010, 04:58
I think that it is exactly the solution. Take a look at the Basic Sort/Filter Model and Address Book Example in the docs (http://doc.qt.nokia.com/4.6/examples-itemviews.html). In the Basic example imagine that your combo box replaces the "Filter Pattern" text box and the the Sorted/Filter table view is your detail panel.

8th June 2010, 05:17
You can update your QRelationalTableModel by using QSqlRelationalTableModel::setFilter()

for example:

void Widget::priceListIndexChanged(int index)
int priceListId = priceListModel->index(index, 0).data(Qt::DisplayRole).toInt();