Results 1 to 6 of 6

Thread: Select a row from QTreeView

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Select a row from QTreeView

    I have a QTreeView with the model set from a QSqlQueryModel.
    Now I need to select the first row when the query is run.
    I can't find any way to do this programatically.
    QTreeWidget has setCurrentItem() and some other means, but QTreeView and QSqlQueryModel don't seem to have any methods for selection. Do I have to resort to using a QTreeWidget ?

    BTW: Found this in the docs, but it doesn't select anything:
    Qt Code:
    1. QModelIndex first = model->index(0, 0, QModelIndex());
    2. treeView->setCurrentIndex(first);
    To copy to clipboard, switch view to plain text mode 
    Last edited by vieraci; 10th July 2009 at 06:22. Reason: updated contents

  2. #2
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Solved: Select a row from QTreeView

    Yes the code DOES work, it was in the wrong order (before I ran the query)

  3. #3
    Join Date
    Aug 2015
    Posts
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Select a row from QTreeView

    Hi vieraci,

    Can you please show me the snippet of code which is working?
    I am facing the similar issue.


    Thanks

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Select a row from QTreeView

    I am sure that vieraci has been waiting for 6 1/2 years for someone to ask that.

  5. #5
    Join Date
    Dec 2015
    Posts
    4

    Default Re: Select a row from QTreeView

    Qt Code:
    1. QPoint p(0,27 * [RowNumber]);
    2. ui->tree_Widget->setCurrentIndex(ui->tree_Widget->indexAt(p));
    To copy to clipboard, switch view to plain text mode 

    You can use this. Its not perfect, but it works. Maybe you have to change the 27.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Select a row from QTreeView

    You can use this. Its not perfect, but it works. Maybe you have to change the 27.
    Sorry, but that is not a good answer. It depends on font size, row height, and any number of other variables unique to the PC where the app is running. On your PC, maybe the height of a row is 27, but on my PC, it probably isn't. The "indexAt( const QPoint & )" method is generally used to let the application know where a user clicked with the mouse, not in the way you are suggesting.

    The answer in the original post is correct and should be used.

Similar Threads

  1. QTreeView Problem
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 7th March 2009, 11:32
  2. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 22:22
  3. background image in QTreeView
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 07:25
  4. Selection problem in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 7th October 2006, 17:02
  5. QSortFilterProxyModel & QTreeView
    By Bear in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 16:04

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.