• I have a Qtreeview displaying a custom QSortFilterProxyModel based on the QFileSystemModel. I need to filter out rows within my root directory. What can I use to filter only files within the rootdirectory that I set (QModelIndex? cast my sourcemodel to QFileSystemModel and compare file name?)? What would be the ideal way to implement this?

  • I think that is the only option.
    You could also compare the parent model index with the root model index, but no way around casting the source model.

  • No need for casts, I think. Why not just make the role to apply the filter on a property of the QSFPM?

  • And which property should be used?
    Do you mean: use data() with a specific column? there you don't get the full path, right? So how to check thge parent directory of an element?

  • Sure you have! See the QFileSystemModel::Roles enum.
    Using the right role, you have the full path of every item in the model directly from that item.

  • Andre, can you provide code snippet? Let's say I have the following directory and I wanted a filter to do the folders below:
    TestSuite <--- Root Directory
    -- common <--- need to filter this folder
    -- tools <--- need to filter this folder
    -- test1 <--- keep this folder
    -- test2 <--- keep this folder
    L cfg <--- need to filter this subfolder
    L testa <--- keep this subfolder
    L testb <--- keep this subfolder

    Basically the logic for my filter will be 1.) Child of root directory and 2) Subdirectory folder must start with test.

  • I could, but I won't, sorry. You learn nothing of just copy/pasting snippets from others. The idea is that you will have to do it, so that you learn. What have you tried so far?

  • Andre, I was not trying to copy your source by any means. I was hoping your example can help answer some of things I am unsure how to implement efficiently. The following is what I currently have:

    bool CheckableProxyModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
    QModelIndex index0 = sourceModel()->index(source_row, COL_FILE_NAME, source_parent);
    QString str = ((QFileSystemModel)sourceModel()).fileName(index0);

    qDebug() << "filterAcceptsRow 0: " << str;
    if (str == "/")
        return true;
    str = ((QFileSystemModel)sourceModel()).filePath(index0);
    if (str.contains(root))
        qDebug() << "filterAcceptsRow 1: " << str;
        str = str.remove(0, root.length());
        qDebug() << "filterAcceptsRow 2: " << str;
        if (str == "" || str.startsWith("test_"))
            return true;
    return false;


    Can you help clarify the following:
    1.) I am currently using a fixed string for my root directory comparison. Is there a way for me to retrieve it from my source model?
    2.) How do I iterate through the subdirectories for line 21 and filter out the rows that starts with "test_"

    You help is greatly appreciated!

  • Andre, I got things to work like I wanted. However, what is the best way for me to determine my source model root directory? Do I have to set it manually or can I determine it from my source model?

Log in to reply

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