Important: Please read the Qt Code of Conduct -

CheckableProxyModel - saving/loading check states

  • Hello,

    I'm using the Andre Somers 's CheckableProxyModel to add the possibility to check elements in a QTreeView. I'm using QFileSystemModel as model but what I'd like to achieve could work with any models (lazzy or not).
    I'd like to be able to save and load check state. I 'm not sure what direction I should choose.
    I'm saving the current states as an include/exclude model.

    • include c:\test_folder
    • include c:\users\bruno\desktop
    • exclude c:\users\bruno\desktop\file.txt

    Should I expand every node referenced in my include/exclude list to apply the states or could I overload CheckableProxyModel::resolveCheckStateRole to compute the state based on the list ?

    Any other idea are welcome!! :)

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What you say saving and loading, do you mean like in saving the state to a file, then read it again and modify your model ?

  • Hi. Thank you.

    Yes. Sorry if I'm not really clear. The purpose is to load/restore checked states on a QTreeView based on a QFileSystemModel. I'm using the excellent Andre Somers 's CheckableProxyModel class for the checkboxes part.
    I'm really new to Qt and I'm Learning the nice Qt's model/view. I'm fan!

    So let's try to explain more my problem:
    I want to restore, a previous checked state, saved on sqlite db, on my file system tree.
    Since QFileSystemModel is a "lazy" model, the full file system isn't loaded on the model. It's populated each time we expand a node. The goal is to load the checked state in a lazy manner too.

    I implemented an (uggly) alternative way: expanding each node referenced in my "checked list" and checkit.
    the QModelIndex get by myQFSM->index("C:\Toto\Titi.txt") doesn't have a valide row/column index if the parents are not deployed yet.
    So here the trick in a thread:

     QSqlQuery query(sQuery);
            while (
                // paths are stored like this:
                // C:|Folder|File.txt
                QString path = query.value(0).toString().replace('|', QDir::separator());
                // let's split the path
                QStringList nodeString = path.split(QDir::separator(), QString::SkipEmptyParts);
                QString name = "";
                QString tmppath = "";
                uint uCount = 0;
                foreach (name, nodeString)
                    qDebug() << name;
                    tmppath += name;
                    // add separator is we're not on a leaf (file)
                    if(uCount < nodeString.size() - 1)
                        tmppath += QDir::separator();
                    // let's take index from our QFSM model
                    QModelIndex idx = _foldermodel->index(tmppath, 0);
                    if(!idx.isValid()) continue;
                    // map it on our proxy model (the one used in our QTreeView and checkbox)
                    QModelIndex index = _filter->mapFromSource(idx);
                        // found somewhere scrollto is waiting for a full populated node
                        // TODO: better to use the QFileSystemModel::directoryLoaded signal
                        // We are now on our checked item... let's check it!
                        if(uCount == nodeString.size() - 1)
                            _filter->setCheckedState(index, true);

    It is doing the job, it's pretty fast... but it's not very elegant.

    The lazy way should bring an other problem: maintain the full/half checked for parents. We need to check if a parent (contained our checked list) dosn't have unchecked item.

    If someone have a nice idea, I'll be happy to look at it. If not I'll use the uggly way :)

  • Lifetime Qt Champion

    One possibility might be to use directoryLoaded.