Qt World Summit: Submit your Presentation

QTreeView QFileSystemModel .. edit item displayed name

  • Hello,

    I'm a little bit lost regarding QTreeView, QFileSystemModel, QSortFilterProxyModel and QModelIndex on Qt 5.1.0

    I want to do a custom file browser on my app..

    The folder to be displayed should respect a certain hierarchy... I manage to do that by using QSortFilterProxyModel and reimplementing filterAcceptsRow ...prefect...

    But know... I want to change the way filenames are displayed.... example : "AA1234_1.txt" and its brother item "AA1234_2.txt" should be displayed by just one item "AA1234" in the file browser

    Do you think it's possible ?

    I tried sourceModel()->setData(index0,QVariant("AA1234"),Qt::DisplayRole); in my function filterAcceptsRow

    but it's rejected...

  • Moderators

    instead of setting the data you should return true for the first file and for the second (all other files which you want to bundle together) return false. So they get hidden.
    Then overload the data() method and filter out the part which you want to strip out of the file name.

  • Thanks for your answer.

    Yes indeed, I managed to hide the others files... but can't manage to change the display name

    ... how should I do to overload data() ?
    Isn't it the method supposed to give the name...
    How do I retrieve the name coming in ? (am I clear ?)

  • Moderators

    call the base class implementation and edit it's return value. Return the edited value...

  • All Right !!! I got it ! Not perhaps the sexiest way of doing it but anyway, thanks for your time answering me, it made me think a little further !
    QVariant MyItemModel::data(const QModelIndex &index, int role) const
    if (!index.isValid())
    return QVariant();

    QVariant initial_value;
    initial_value = QFileSystemModel::data(index,role);

    if (role == Qt::DisplayRole)
    QString czName = initial_value.toString();

    if (czName.right(4).compare(".txt") == 0)

    return initial_value;

Log in to reply