Results 1 to 3 of 3

Thread: QT SqlQueryModel, TreeView and SortFilterProxyModel how to get selected row

  1. #1
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QT SqlQueryModel, TreeView and SortFilterProxyModel how to get selected row

    I have a TreeView that is feeded by a SortFilterProxyModel that is feeded by a SqlQueryModel.

    Now I want to add double click event so that an edit dialog is loaded with data from the selected row and can be edited.

    But all I get is a "random" row to be loaded, it's like the TreeView current index is wrong. My guess is that the SortFilterProxyModel is messing it up, but I have no clue on how to get the right index.

    This is how I set my models:
    Qt Code:
    1. proxyModel = new SortFilterProxyModel();
    2.  
    3. treeView = new QTreeView();
    4. treeView->setModel(proxyModel);
    5.  
    6. sqlModel = new QSqlQueryModel(this);
    7. proxyModel->setSourceModel(sqlModel);
    To copy to clipboard, switch view to plain text mode 

    And this is my code that gets the wrong row:

    Qt Code:
    1. QSqlRecord product = sqlModel->record(treeView->currentIndex().row());
    To copy to clipboard, switch view to plain text mode 

    I'm a newbie on QT but I've looked everywhere on the net and couldn't find an answer, so I'm hoping someone here can help me!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT SqlQueryModel, TreeView and SortFilterProxyModel how to get selected row

    You need to take the view's currentIndex(), which is a model index for the sorted/filter side of the proxy model, and use the proxy model's QSortFilterProxyModel::mapToSource() function to obtain a corresponding model index for the source model. Then you can use that index (if valid) to address the SQL model directly.

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

    LautaroMed (1st May 2013)

  4. #3
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QT SqlQueryModel, TreeView and SortFilterProxyModel how to get selected row

    Thank you so much! You just made my day :P

Similar Threads

  1. Replies: 1
    Last Post: 7th August 2012, 12:41
  2. Replies: 1
    Last Post: 4th August 2011, 22:36
  3. How to get parent of selected item in TreeView?
    By RavenS in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2009, 13:27
  4. TableView + SqlQueryModel: can't insert or edit rows
    By jiveaxe in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2008, 21:55
  5. SortFilterProxyModel data
    By gyre in forum Newbie
    Replies: 5
    Last Post: 6th December 2007, 22:09

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.