Results 1 to 6 of 6

Thread: QTreeView - how to autoselect first row?

  1. #1
    Join Date
    Jul 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTreeView - how to autoselect first row?

    Hi Everyone,

    I have a QTreeView that displays some directory content using an underlying QDirModel. My problem is that when it is displayed, none of the rows is selected (ie. highlighted) in the view - I have to click on the first row for example to do that.

    I want however the first row selected by default when the view shows up.

    Tried some examples from the net but none worked...

    Does anyone know how to achieve this?

    Thanks for your help,

    Istvan

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeView - how to autoselect first row?

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication app(argc,argv);
    6. QTreeView view;
    7. QDirModel model;
    8. view.setModel(&model);
    9. view.setCurrentIndex(model.index(0, 0));
    10. view.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView - how to autoselect first row?

    As a side note: I suggest to use

    Qt Code:
    1. view->setCurrentIndex(view->model()->index(0, 0));
    To copy to clipboard, switch view to plain text mode 

    That way your code will work even when you finally decide you want to wrap your model in a QSortFilterProxyModel.

    The compiler can not give you safety in the sense that a QModelIndex, a model and a view actually match (was the QModelIndex an index of the proxy or the source model again...).
    Using view->model() etc makes your code improves maintainability by being more robust against such errors and model changes. And in my experience most views will sooner or later get a proxy round their model.

    HTH

  4. #4
    Join Date
    Jul 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView - how to autoselect first row?

    Thanks for your help!

    My problem is, that when I use 3 additional lines in order to have the treeview show the entries in full row style, the autoselection does not work anymore:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication app(argc,argv);
    6. QTreeView view;
    7. QDirModel model;
    8.  
    9. view.setModel(&model);
    10.  
    11. // To enable full-row style:
    12. view.setRootIndex(model.index(QDir::homePath()));
    13. view.setItemsExpandable(false);
    14. view.setRootIsDecorated(false);
    15.  
    16. view.setCurrentIndex(model.index(0, 0));
    17.  
    18. view.show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    I used the whole stuff with these settings originally - I never tried without these 3 lines before, so I did not see it work...

    Any idea?

    Thanks
    Last edited by jpn; 24th July 2008 at 21:48. Reason: missing [code] tags

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Re: QTreeView - how to autoselect first row?

    that's because model.index(0,0) is not part of the subtree shown in the view (after you have called setRootIndex).
    Try

    Qt Code:
    1. view.setCurrentIndex(model.index(0, 0, view.rootIndex()));
    To copy to clipboard, switch view to plain text mode 
    i.e. the first index below the root (whatever that is)

    HTH

    PS: this code is somewhat fragile if you happen to introduce a filter proxy later
    Last edited by caduel; 24th July 2008 at 22:12. Reason: fix typo

  6. #6
    Join Date
    Jul 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView - how to autoselect first row?

    Thanks a lot: that works finally!

    view.setCurrentIndex(model.index(0, 0, model.index(QDir::homePath())));

    works just the way I want. And thanks for the explanation, too - I haven't fully understood all the aspects of indexing in the model/view arch. yet.

    Thanks all of you for the comments!

    Istvan

Similar Threads

  1. QTreeView repaint
    By graeme in forum Qt Programming
    Replies: 17
    Last Post: 13th March 2012, 13:27
  2. QDirModel and QTreeView cut and paste
    By Micawber in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2008, 20:16
  3. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 21:22
  4. background image in QTreeView
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 06:25
  5. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24

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.