Results 1 to 3 of 3

Thread: jump to index in QDataWidgetMapper that uses QSqlTableModel

  1. #1
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default jump to index in QDataWidgetMapper that uses QSqlTableModel

    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:

    Qt Code:
    1. //lineEdit 'jump' button <enter> pressed
    2. void MainWindow::on_le_jump_returnPressed()
    3. {
    4. QModelIndex indx = completer->currentIndex();
    5. if (!indx.isValid())
    6. qDebug() << "is null";
    7.  
    8. //column 0 is the unit name, column 1 is the record index
    9. mapper->setCurrentIndex(completer->currentIndex().sibling(0,1).data().toInt());
    10. ui->le_jump->clear();
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. SELECT LastName, UnitName, UnitID FROM unit WHERE LastName LIKE '%%%1%%'
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by schnitzel; 11th June 2011 at 22:19.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: jump to index in QDataWidgetMapper that uses QSqlTableModel

    I would probably use QSortFilterProxyModel instead of filtering via SQL.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    schnitzel (15th June 2011)

  4. #3
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: jump to index in QDataWidgetMapper that uses QSqlTableModel

    Quote Originally Posted by wysota View Post
    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.

Similar Threads

  1. Replies: 0
    Last Post: 17th October 2010, 12:53
  2. Replies: 3
    Last Post: 4th October 2010, 03:57
  3. Replies: 1
    Last Post: 4th October 2010, 00:46
  4. QDataWidgetMapper setting own model index
    By AlGaN in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2010, 16:34
  5. Replies: 3
    Last Post: 31st March 2008, 21:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.