Results 1 to 3 of 3

Thread: Problem getting selected item from QTreeView + QSortFilterProxyModel

  1. #1
    Join Date
    Dec 2014
    Posts
    11
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Question Problem getting selected item from QTreeView + QSortFilterProxyModel

    Hi,

    I'm not being able to read the selected item from a QTreeView when it's being sorted by a QSortFilterProxyModel. All the references from currentIndex(), currentIndex().row(), selectionModel()->currentIndex() or whatever are always returning the row number in QTreeview, but I cannot find the correct record in the original model using the row number as the sorting process seems to be not affecting the sequence of data in the model. I'm using the QSqlQueryModel.

    Am I doing something wrong here? I would appreciate any suggestion....

    Thank-you,
    Renato A. Ferreira

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting selected item from QTreeView + QSortFilterProxyModel

    The indexes returned by the view's selection model are obviously from the model that the view works on.
    In your case the proxy model.

    They don't make any sense in the context of the proxy's source model.

    If you really need an index of the source model, use the proxy's mapToSource() function.

    If you just need data from the index, use its data() function.

    Cheers,
    _

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

    RenatoFerreira (25th December 2014)

  4. #3
    Join Date
    Dec 2014
    Posts
    11
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem getting selected item from QTreeView + QSortFilterProxyModel

    Perfect! It worked with mapToSource()...

    Qt Code:
    1. mainModel.record(proxyModel->mapToSource(ui->treeView->currentIndex()).row())
    To copy to clipboard, switch view to plain text mode 

    And now I can access the primary key to search the rest of data from database.

    Thank-you!

Similar Threads

  1. How to keep selected item in QTreeView
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2011, 05:16
  2. Replies: 2
    Last Post: 17th February 2011, 02:55
  3. Changing selected QTreeView item font
    By dima1000 in forum Newbie
    Replies: 5
    Last Post: 8th October 2009, 09:28
  4. QTreeView, QSortFilterProxyModel and item expansions
    By benacler in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2008, 20:30
  5. QTreeView: selection behavior upon selected item removal
    By Pieter from Belgium in forum Qt Programming
    Replies: 6
    Last Post: 11th July 2007, 16:00

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.