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. QFileSystemModel - iterating through current index children
Forum Updated to NodeBB v4.3 + New Features

QFileSystemModel - iterating through current index children

Scheduled Pinned Locked Moved General and Desktop
68 Posts 7 Posters 48.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.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #37

    Correct! The default state would have to be checked. Can you add the methods to get unchecked files? Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #38

      [quote author="phamtv" date="1304007771"]Correct! The default state would have to be checked. Can you add the methods to get unchecked files? Thanks![/quote]

      Done. :-)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #39

        I have created a "repository":https://gitorious.org/checkableproxymodel on gitorious for this project. The licence for this project is GPL-3. Note that CheckableProxyModel makes use of my "DelayedExecutionTimer class":http://developer.qt.nokia.com/wiki/Delay_action_to_wait_for_user_interaction that I published earlier on this site (BSD).

        I have also made a separate "announcement":http://developer.qt.nokia.com/forums/viewthread/5531/ in the showcase section.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          phamtv
          wrote on last edited by
          #40

          I sincerely appreciate your time and effort of support! This is a major piece of the puzzle for the application I am currently working on. Thank You!!!!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #41

            You're welcome. I always like a good challenge, and this question was quite interesting.

            Please let me know what you think of the code, the API and inform me of any issues you run into with this code. Also, I hope you can follow how this actually works. If you enable the debug information, you can actually see of how many items the state is stored. You'll see that that is not so many...

            Just out of curiousity: what application are you working on?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              phamtv
              wrote on last edited by
              #42

              I am working on an application that will be the main interface for our engineers to add, delete, and edit test modules written in Expect/tcl. It will have the capability to execute and interact with an external process called dejagnu that will run our automated roadtest modules to test our embedded devices.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #43

                Open source, or in-house use only?

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  phamtv
                  wrote on last edited by
                  #44

                  In house only...

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    phamtv
                    wrote on last edited by
                    #45

                    Hi Andre,

                    when I set the model of my QTreeView to the CheckableProxyModel, how can I determine if the QModelIndex I just double-clicked on is a Directory or not? My previous implementation prior to setting the model to CheckableProxyModel is as follow:
                    @
                    void TreeEditor::openFile(const QModelIndex& index)
                    {
                    if(index.isValid() && index.model())
                    {
                    QFileSystemModel* model = (QFileSystemModel*)index.model();

                        if(!model->isDir(index))
                        {
                            QString filePath = model->filePath(index);
                            open(filePath);
                        }
                    }
                    

                    }
                    @

                    Do I have add a function to the CheckableProxyModel class to find out if the index I clicked on is a directory?

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #46

                      Keep using isDir, but map the item index before querying it. At least, that is how I would do it. I guess a branch node in the tree is not in all cases the same as a node representing a directory.

                      I would keep this functionality out of CheckableProxyModel. Just let that class do what it does. If you add this, you will bind it to be only usable in combination with a QFileSystemModel. That would be a shame.

                      @

                      void TreeEditor::openFile(const QModelIndex& index)
                      {
                      if(index.isValid() && index.model())
                      {
                      QFileSystemModel* model = m_fileSystemModel; // just keep a pointer around
                      CheckableProxyModel* proxy = m_checkableProxyModel; //same here

                          if(!model->isDir(proxy->mapToSource(index)))
                          {
                              QString filePath = index.data(QFileSystemModel::FilePathRole).toString();
                              open(filePath);
                          }
                      }
                      

                      }
                      @

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        phamtv
                        wrote on last edited by
                        #47

                        I understand that this is pertaining only to QFileSystemModel so I don´t want to add it to the CheckableProxyModel class, but how can I dynamically populate the QTreeView with only the directory I am interested in. My previous code to set the current directory as the root of the QTreeView was as follow:

                        @
                        QDir::setCurrent(strCurrTestDir);
                        QModelIndex idx = fsModel->index(strCurrTestDir);
                        fileTree->setRootIndex(idx);
                        @

                        Resolved:
                        @
                        QDir::setCurrent(strCurrTestDir);
                        QModelIndex idx = fsModel->index(strCurrTestDir);
                        fileTree->setRootIndex(m_checkProxy->mapFromSource(idx));
                        @

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          phamtv
                          wrote on last edited by
                          #48

                          Hi Andre,

                          your implementation of the Qtree´s checkstate propogation works great!!! Thank you!

                          One specific question related to my project is that since I am interested in getting the unchecked files for processing. What would be the quickest method for me to get all of the unchecked filenames under the unchecked directories? Should I be using QDirectoryIterator?

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #49

                            Yes. Get the unchecked directories from the proxy model, and iterate over those directories using a QDirectoryIterator.

                            I am glad my proxy model works for you!

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              rawfool
                              wrote on last edited by
                              #50

                              Hi Andre,

                              I'm trying to understand your checkableproxymodel code. It's very well written & thanks for the same.
                              In mainwindow.cpp, selectedItemsChanged() function, I see the path of checked & unchecked files/directories. Also I want the size of checked files. How to get the size of the file I check ?

                              Kindly help me. Thank you.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #51

                                [quote author="rawfool" date="1399037548"]Hi Andre,

                                I'm trying to understand your checkableproxymodel code. It's very well written & thanks for the same.
                                In mainwindow.cpp, selectedItemsChanged() function, I see the path of checked & unchecked files/directories. Also I want the size of checked files. How to get the size of the file I check ?

                                Kindly help me. Thank you.[/quote]

                                That's really a bit besides the point of the model, but ok. The model only provides a proxy on top of any other tree-type model you put behind it. If that model is a model describing the file system, then you can use it to access information on files. If it is something else, then, well...

                                In the example, the base model is a QFileSystemModel. QFileSystemModel provides roles for the path, the icon and the permissions, but not for the file size (weird omission, I think). The size is available from column 1 in the model though, but as a string, not as a value in bytes. To get that data, you have to use something like this:

                                @
                                foreach (const QModelIndex index, selectedFiles) {
                                list += "<li>" + index.data(QFileSystemModel::FilePathRole).toString() + "</li>";
                                QModelIndex sizeIndex = index.model().index(index.row(), 1, index.parent());
                                qDebug() << "size of" << index.data(QFileSystemModel::FilePathRole)
                                << "is" << index.data();
                                }
                                @
                                (note: completely untested code)

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  rawfool
                                  wrote on last edited by
                                  #52

                                  Hi Andre, thanks for replying.
                                  I'm getting an error after adding the above code and it says -

                                  @error: request for member 'index' in 'index.QModelIndex::model()', which is of non-class type 'const QAbstractItemModel*'@
                                  for the line -
                                  @QModelIndex sizeIndex = index.model().index(index.row(), 1, index.parent());@

                                  I'm novice to model view concepts, so unable to solve this. Can you help me make it right. Thank you.

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andre
                                    wrote on last edited by
                                    #53

                                    That's why I said: untested code. It is meant as a pointer (pun intended), not as a ready-made solution.

                                    So, use -> instead of .

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      rawfool
                                      wrote on last edited by
                                      #54

                                      Hi Andre,

                                      In selectedItemsChanged() slot, instead of iterating through all the checked/unchecked items, is there a way to get only checked/unchecked/modified item?

                                      Thank you.

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        andre
                                        wrote on last edited by
                                        #55

                                        So, you want to know what changed exactly? No, there is currently no API for that. I guess it could be added by modifying the setCheckState method.

                                        1 Reply Last reply
                                        0
                                        • R Offline
                                          R Offline
                                          rawfool
                                          wrote on last edited by
                                          #56

                                          Thank you for responding.

                                          Yeah, I want to know what has changed in the tree.
                                          Actually I'm adding the checked items to a QTableView. On every check/uncheck of file or directory, I'm adding the same to QAbstractTableModel based class.

                                          For every update, I'm clearing the data and adding again in foreach loop.
                                          So, the problem is if there are 100000 files in a directory, and if 99998 files are checked and for another check, I will have to delete all the data, again iterate through all the 99998+1 items, which is a burden.

                                          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