Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. CheckableProxyModel - saving/loading check states
Forum Updated to NodeBB v4.3 + New Features

CheckableProxyModel - saving/loading check states

Scheduled Pinned Locked Moved Unsolved General and Desktop
checkableproxymqsortfilterproxqtreeviewload checkstate
4 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • OrkbluttO Offline
    OrkbluttO Offline
    Orkblutt
    wrote on last edited by
    #1

    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.
    Example:

    • 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!! :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • OrkbluttO Offline
        OrkbluttO Offline
        Orkblutt
        wrote on last edited by
        #3

        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 (query.next())
                {
                    // 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);
                        if(index.isValid())
                        {
                            ui->treeView->expand(index);
                            // found somewhere scrollto is waiting for a full populated node
                            // TODO: better to use the QFileSystemModel::directoryLoaded signal
                            ui->treeView->scrollTo(index);
        
                            // We are now on our checked item... let's check it!
                            if(uCount == nodeString.size() - 1)
                            {
                                _filter->setCheckedState(index, true);
                                ui->treeView->collapseAll();
                            }
                        }
                        uCount++;
                    }
                }
        

        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 :)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          One possibility might be to use directoryLoaded.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved