Results 1 to 6 of 6

Thread: QTreeView - how to autoselect first row?

Hybrid View

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

    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

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

    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

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
  •  
Qt is a trademark of The Qt Company.