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 customization
Qt 6.11 is out! See what's new in the release blog

QFileSystemModel customization

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 7.7k Views 2 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.
  • Z Offline
    Z Offline
    Zbigniew-Sch
    wrote on last edited by
    #11

    You can sort and remove data using QSortFilterProxyModel,
    but how can I insert or remove header columns in tree view using a proxy?
    Please provide an example (no long comments !!!).

    SGaistS JonBJ 2 Replies Last reply
    0
    • Z Zbigniew-Sch

      You can sort and remove data using QSortFilterProxyModel,
      but how can I insert or remove header columns in tree view using a proxy?
      Please provide an example (no long comments !!!).

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #12

      @Zbigniew-Sch said in QFileSystemModel customization:

      You can sort and remove data using QSortFilterProxyModel,
      but how can I insert or remove header columns in tree view using a proxy?
      Please provide an example (no long comments !!!).

      As @JonB wrote, exactly how you did it with subclassing QFileSystemModel.

      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
      1
      • Z Zbigniew-Sch

        You can sort and remove data using QSortFilterProxyModel,
        but how can I insert or remove header columns in tree view using a proxy?
        Please provide an example (no long comments !!!).

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #13

        @Zbigniew-Sch
        As @SGaist and I are suggesting, you should be able to do all your work just as easily with a proxy model having the QFileSystemModel as its source as sub-classing QFileSystemModel to do same. It just seems preferable here to stick with just the supplied QFileSystemModel and put a proxy on top of it rather than creating a new sub-classed model. However, don't get hung up on it, if you really prefer or want to try sub-classing you can do that. If all is normal you can always swap between doing it as a proxy or as a sub-class with just about no code change.

        However, as mentioned before if all you want to do is hide some of the default columns or show/hide some additional ones of your own you may be better doing this at the treeview I level via its setColumnHidden(). Unless you allow your users to actually define a new column at runtime (somehow) I would imagine your case is:

        • You start with whatever columns QFileSystemModel provides (maybe about 4?).
        • You choose to add --- either in the proxy or a subclass --- some more pre-set columns of your own. Now there are, say, 8 possible columns in total.
        • Rather than deleting or adding from these columns at runtime in response to some user action, leave all columns permanently available in the proxy/sub-class model and instead use show/hide column from the treeview.
        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          Zbigniew-Sch
          wrote on last edited by
          #14

          Regarding your suggestion:
          Rather than deleting or adding from these columns at runtime in response to some user action, leave all columns permanently available in the
          proxy/sub-class model and instead use show/hide column from the treeview.

          please write an example, then I'll understand (no long comments !!!).

          JonBJ 1 Reply Last reply
          0
          • JonBJ JonB

            @Jo-Jo said in QFileSystemModel customization:

            Which methods should I implement in MyCoolProxy?

            @Jo-Jo said in QFileSystemModel customization:

            Name 2) Size 3) Type 4) Date Modified
            How can I add new or remove default columns? Here is my code

            The ones to give you these. For example, adding some new column of yours would require you to override columnCount() to return 1 more than it currently does. Then you'll want to alter headerData() to return the title for the new column and data() to return the value for the cell for your new column at whatever row. And if you wanted to "delete" a column the same thing in reverse, like decrementing columnCount().

            Note that if all you want to do is selectively hide or show columns that can also be done via QTreeView::setColumnHidden(), you don't have to have a proxy just for that.

            J Offline
            J Offline
            Jo Jo
            wrote on last edited by
            #15

            @JonB said in QFileSystemModel customization:

            The ones to give you these. For example, adding some new column of yours would require you to override columnCount() to return 1 more than it currently does. Then you'll want to alter headerData() to return the title for the new column and data() to return the value for the cell for your new column at whatever row.

            That's all? Should I implement some other methods?

            JonBJ 1 Reply Last reply
            0
            • Z Zbigniew-Sch

              Regarding your suggestion:
              Rather than deleting or adding from these columns at runtime in response to some user action, leave all columns permanently available in the
              proxy/sub-class model and instead use show/hide column from the treeview.

              please write an example, then I'll understand (no long comments !!!).

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #16

              @Zbigniew-Sch
              Exactly your example just as you presented it above at https://forum.qt.io/post/835731! Just that rather than your

              implement own QFileSystemModel class (class derived from QFileSystemModel )

              @SGaist and I would rather do it via a proxy model per his https://forum.qt.io/post/835716. If the proxy is such a big/confusing issue then at this point I don't care, do it via sub-classing if you prefer to get through this stage.

              That has allowed for however many (just 1 in your code) columns to be added before you start out. Then I would not add or remove columns from the subclassed or proxy model after that, rather I would use void QTreeView::setColumnHidden(int column, bool hide) to hide or show among all the columns from the subclass/proxy as desired.

              1 Reply Last reply
              0
              • J Jo Jo

                @JonB said in QFileSystemModel customization:

                The ones to give you these. For example, adding some new column of yours would require you to override columnCount() to return 1 more than it currently does. Then you'll want to alter headerData() to return the title for the new column and data() to return the value for the cell for your new column at whatever row.

                That's all? Should I implement some other methods?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #17

                @Jo-Jo said in QFileSystemModel customization:

                That's all? Should I implement some other methods?

                That depends what you want to do! So far as I can see you have everything you need at this stage to add new available columns at code time (per @Zbigniew-Sch) and attach the proxy (per @SGaist, subclass from QIdentityProxyModel). Have you actually tried it just as shown? Then you will know/see whether it works for adding one available column. No point asking about "other methods" if that works until you want something else!

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jo Jo
                  wrote on last edited by
                  #18

                  @JonB Yes, I tried it and it works. I’m asking because I think there might be some UB if I don’t implement some other methods

                  JonBJ 1 Reply Last reply
                  0
                  • J Jo Jo

                    @JonB Yes, I tried it and it works. I’m asking because I think there might be some UB if I don’t implement some other methods

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #19

                    @Jo-Jo
                    Well, you have enough for what you use now! You will only need more methods if you want to do other things. Remember that --- if you are using the proxy approach? --- by starting from QIdentityProxyModel you begin with everything properly "fixed up" for an identity proxy model. No UB there! Then you are only adding your stuff on top. If you had started with a plain QAbstractProxyModel then indeed you would have had quite a bit of "groundwork" to do:

                    All standard proxy models are derived from the QAbstractProxyModel class. If you need to create a new proxy model class, it is usually better to subclass an existing class that provides the closest behavior to the one you want to provide.

                    You chose QIdentityProxyModel, the best starting point if you are going to be adding facilities over existing base behaviour, while say QSortFilterProxyModel does so much different from the base that it does not start from QIdentityProxyModel. Have a look at https://doc.qt.io/qt-6/qidentityproxymodel.html to see just how many method reimplementations that has done for you. Or at its source code if you want to see what they have to "fix up" to get everything working.

                    1 Reply Last reply
                    1
                    • J Offline
                      J Offline
                      Jo Jo
                      wrote on last edited by
                      #20

                      @JonB Thank you!

                      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