Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to programattically select row in QTreeView?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to programattically select row in QTreeView?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 9.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    @
    connect(dirmodel,
    SIGNAL(directoryLoaded(QString)),
    this,
    SLOT(model_directoryLoaded(QString)));@


    Context: For a Qt learning project I am building an image viewer. I have a 'QTreeView' with a backing 'QFileSystemModel.' The user has the option to select a directory of images and only the contents of that directory will be displayed in the QTreeView (i.e. it is not displaying the entire file system, just the subdirectory the user selects).

    I would like to have the first item in the QTreeView selected when the user changes directories. I have seen this question (http://stackoverflow.com/questions/4967660/selecting-a-row-in-qtreeview-programatically) but it didn't work for me. I've been poking at this for a few hours now. What am I doing wrong? I am able to select directories and display the images but I am missing how to set the QTreeView selection.

    My code is below, with various attempts commented out. The console output from the qDebug statement is always:

    next_index -1 -1

    @
    void DirBrowser::on_actionSet_Image_Directory_triggered()
    {
    QString dPath = QFileDialog::getExistingDirectory(this,
    tr("Set Image Drectory"), QDir::currentPath());

        if (!dPath.isEmpty()) {
    
            QModelIndex idx = dirmodel->setRootPath(dPath);
            myTreeView->setRootIndex(idx);
            myTreeView->setCurrentIndex(idx);
    
            QModelIndex next_index =myTreeView->currentIndex().child(1,0);
         
            //QModelIndex next_index = myTreeView->model()->index(1, 0);
            //QModelIndex next_index =idx.child(0,0);
            qDebug() << "next_index" << next_index.row() << next_index.column();
            //myTreeView->setCurrentIndex(next_index);
    
            myTreeView->selectionModel()->select(next_index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    
            QString path = dirmodel->fileInfo(next_index).absoluteFilePath();
            qDebug() << "set Dir" << path;
        }
    }
    

    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You have found somehow "currentIndex":http://qt-project.org/doc/qt-5.0/qtwidgets/qabstractitemview.html#currentIndex already. The documentation there refers to "setCurrentIndex.":http://qt-project.org/doc/qt-5.0/qtwidgets/qabstractitemview.html#setCurrentIndex

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        (Little help?)

        That is not working for me. Perhaps someone could give me a more detailed reply? This can't be all that hard to accomplish.

        Here is what I am doing:

        I pass an absolute path which points to a directory of images and set the root path of my QFileSystemModel instance and use the index returned by that to set my QTreeView instance root index
        @
        QModelIndex idx = dirmodel->setRootPath(dPath);
        myTreeView->setRootIndex(idx);
        @

        I then set the current index of the treeview to that index. Which I guess is pointing to the directory – perhaps this is wrong?
        @
        myTreeView->setCurrentIndex(idx);
        @

        I have tried using the selectionModel of the treeView to make the selection using that index:
        @
        myTreeView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
        @

        I have also tried accessing a child of the directory and using that to set the current index and selection:
        @
        QModelIndex next_index =myTreeView->currentIndex().child(1,0);
        @

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved