[SOLVED] 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.

    @
    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;
        }
    }
    

    @


  • Moderators



  • (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);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.