Results 1 to 3 of 3

Thread: [SOLVED] How to programattically select row in QTreeView?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default [SOLVED] How to programattically select row in QTreeView?

    I have a simple image browser app with a QTreeView backed by a QFileSystemModel instance. The user is able select a directory using a QFileDialog. I have it working but I would like to automatically select the first item in the new directory.

    I've searched around and tried many bits of code without success. I am able to set the QTreeView selection through a button press (as well of course as clicking on the QTreeView).

    My code is below. The console output from the qDebug statement:
    idx row 0 0
    first row 0 0
    idx is the selected directory index, and I don't know how to tell the treeView to select the first item within the directory.

    Am I doing something obviously wrong? Any help appreciated as I've been stuck on this a while.
    Setup: OSX 10.7.5 - Qt 5.1 - desktop app.




    Qt Code:
    1. void DirBrowser::loadDirectory(const QString& dirPath)
    2. {
    3. imageDirectoryPath = dirPath;
    4. saveSettings();
    5. QModelIndex idx = dirmodel->setRootPath(imageDirectoryPath);
    6. myTreeView->setRootIndex(idx);
    7. myTreeView->setCurrentIndex(idx);
    8.  
    9. QModelIndex first = myTreeView->currentIndex();
    10.  
    11. qDebug() << "idx" << "row" << idx.row() << idx.column();
    12. qDebug() << "first" << "row" << first.row() << first.column();
    13.  
    14. myTreeView->selectionModel()->select(first,
    15. QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by TOMATO_QT; 30th July 2013 at 01:51. Reason: updated contents

Similar Threads

  1. Select a row from QTreeView
    By vieraci in forum Qt Programming
    Replies: 5
    Last Post: 7th December 2015, 19:44
  2. Replies: 1
    Last Post: 6th July 2011, 06:03
  3. Select all items in QTreeView. Why very slow ?
    By MaxBooster in forum Qt Programming
    Replies: 0
    Last Post: 25th August 2010, 06:12
  4. QTreeView and multi-select of rows
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2008, 02:32
  5. Programattically Clicking an Item in QTreeView
    By johnny_sparx in forum Qt Programming
    Replies: 4
    Last Post: 11th May 2006, 16:26

Tags for this Thread

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.