Nominate our 2022 Qt Champions!

QFileSystemModel: fileName works, filePath and fileInfo crashes

  • Hi!

    I have a crash occuring with QFileSystemModel when I call filePath
    Here is my code :

    fileSystemModel = new QFileSystemModel; //those are declared in a class and this is the constructor
    proxyModel = new QSortFilterProxyModel;
    QModelIndex rootModelIndex = fileSystemModel->setRootPath("Tracks/");


    Here is the slot function for when an element is clicked within the TreeView. Index is the parameter given by the TreeView signal
    // This works :
    qDebug() << fileSystemModel->fileName(index);

    // This crashes :
    qDebug() << fileSystemModel->filePath(index);

    qDebug() << fileSystemModel->fileInfo(index); //crashes too

    Using Qt 4.8 on Linux Mint with Qt Creator.
    Does any of you have any idea of what I could have been doing wrong ?

    Thanks a lot :)

  • Just to be sure, the view shows the proxy model ?
    If it is the case, you should convert the index you are receiving to an index belonging to the source model with proxyModel->mapToSource(index)

    The reason the fileName call works, is because it doesn't check that the index is valid, but simply returns the Qt::DisplayRole role.
    filePath checks that the model of the index is the same as the one the function is called from but with an assertion, so it crashes and fileInfo calls _filePath, so it crashes to.

  • YES! You saved my life. Works perfectly.
    I suspected that it might be due to the different indexes, however I didn't know how to match them.
    Thanks a lot for the explanation :)

Log in to reply