View Full Version : jump to index in QDataWidgetMapper that uses QSqlTableModel

11th June 2011, 22:08
I am using a table with QDataWidgetMapper and QSqlTableModel.
To 'jump' to a particular record, I'm using a QCompleter with QStandardItemModel.
The table contains the fields UnitName and LastName (among others). The QCompleter uses UnitName as the completion field. This all works great, i.e. I can jump to the selected record as follows:

//lineEdit 'jump' button <enter> pressed
void MainWindow::on_le_jump_returnPressed()
QModelIndex indx = completer->currentIndex();
if (!indx.isValid())
qDebug() << "is null";

//column 0 is the unit name, column 1 is the record index

I'm trying to add a dialog where the user can search by (partial) LastName match. I am able to use the following sql:

SELECT LastName, UnitName, UnitID FROM unit WHERE LastName LIKE '%%%1%%'

This query will result in 0 or more matches which are displayed in a QTreeWidget. The user then clicks on one of the rows and this is where I get stuck... How do I now 'jump' to the unit that was just selected?
I tried to use the QCompleter model's match method but I'm not sure if that's the right approach.

I was thinking to set the filter on the mapper but that only works partially, i.e. it selects the correct unit but since the mapper is now filtered, my 'jump to unit' line edit isn't working anymore... I guess I need to clear the filter, but how?

<added a bit later>
I like answering my own question... I simply clear the filter when I use the completer again in the 'jump to unit' line edit -> problem solved.

However, what do you think of my approach? Is there a more elegant solution?

13th June 2011, 10:18
I would probably use QSortFilterProxyModel instead of filtering via SQL.

15th June 2011, 21:53
I would probably use QSortFilterProxyModel instead of filtering via SQL.

Interesting class... thanks for your suggestion. I can probably use this in quite a few places in my app.