Results 1 to 3 of 3

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

  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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to programattically select row in QTreeView?

    Don't you want the index of the first child of the root index not the root index itself?

  3. #3
    Join Date
    Jul 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to programattically select row in QTreeView?

    Figured it out. The problem was that I was trying to make a selection on the `QTreeView` before the the specified directory had finished being loaded. In other words, tryng to make the selection within the method where I was changing directories.

    The solution is to listen for the `directoryLoaded(QString)` `SIGNAL` that `QFileSystemModel` emits.

    void QFileSystemModel::directoryLoaded ( const QString & path ) [signal]
    This signal is emitted when the gatherer thread has finished to load the path.
    This function was introduced in Qt 4.7.

    Qt Code:
    1. connect(dirmodel,
    2. SIGNAL(directoryLoaded(QString)),
    3. this,
    4. SLOT(model_directoryLoaded(QString)));
    To copy to clipboard, switch view to plain text mode 
    Last edited by TOMATO_QT; 30th July 2013 at 01:50. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.