[solved] Find correct index to set selection programmatically



  • I have a QListView and the model is a QFileSystemModel. The view only shows the files in the directory that is currently set as the root directory. If a particular known file name exists in the view list, I need to set that item as selected. I therefore need to run through the file names in the view, or in the QFileSystemModel, in order to get the index for that item to select it. Problem is that I haven't yet found a way of iterating to find the correct index for setting the selection. I have tried a number of different approaches, such as querying the view's model (view->model()) and asking it for the number of children and the row count, but row count is 1 and number of children is 0, while the number of files I see when the view is populated is 6. I also tried
    @
    QString child = (QString)view->childAt(0,0)
    @
    since I know what I'm expecting from the first row, but the string was empty. When I print the root.data() using qDebug(), I get "QVariant(QString, "Root")", which is the name of the directory that contains all the files shown in the view. Does someone have an idea as to how you would go about doing this?


  • Lifetime Qt Champion

    Hi,

    Isn't "QFileSystemModel::index":http://doc.qt.io/qt-5/qfilesystemmodel.html#index-2 what you want ?



  • Wow, that was an oversight. I thought I'd scoured the documentation, but I totally overlooked that. Thanks.


  • Lifetime Qt Champion

    You're welcome !

    If that answers your question, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

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