Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a row in a QTreeview programatically?

Tags:

qt

Update: Let me try to simplify the issue and steps. Does anyone see where I've gone wrong? Also, this is on OSX 10.7.5/Qt 5.1 – perhaps it is an OSX issue?

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);

But I still am not getting the selection on my treeview.


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 (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().sibling(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;
    }
}
like image 370
spring Avatar asked Jul 28 '13 20:07

spring


1 Answers

Figured it out. The problem was that I was trying to make a selection on the QTreeView before 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)));
like image 56
spring Avatar answered Nov 15 '22 09:11

spring