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. How to control collapse/expand in QTreeView

How to control collapse/expand in QTreeView

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 12.0k 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.
  • V Vinoth Rajendran4

    Hi All,
    Is it possible to disable expand/collapse control mechanism for my current directory in QTreeView, even if my current directory has sub-directories and files ???

    My QTreeView has QFileSystemModel as its model.

    Thank You!

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #2

    @Vinoth-Rajendran4
    Well, I don't know if there is a neater way to do it, but I assume you can always define your own slot for http://doc.qt.io/qt-5/qtreeview.html#expand (and others) and have it not call the base slot on certain nodes of your choice?

    V 1 Reply Last reply
    0
    • JonBJ JonB

      @Vinoth-Rajendran4
      Well, I don't know if there is a neater way to do it, but I assume you can always define your own slot for http://doc.qt.io/qt-5/qtreeview.html#expand (and others) and have it not call the base slot on certain nodes of your choice?

      V Offline
      V Offline
      Vinoth Rajendran4
      wrote on last edited by
      #3

      @JonB : Thanks for your reply. Can you please provide an abstract code, of blocking base slot .
      An simple example to get me started.

      JonBJ 1 Reply Last reply
      0
      • V Vinoth Rajendran4

        @JonB : Thanks for your reply. Can you please provide an abstract code, of blocking base slot .
        An simple example to get me started.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #4

        @Vinoth-Rajendran4
        I don't do C++. You'll have to fill in details/corrections.

        1. Don't use a QTreeView directly if you are doing that at present. Create a sub-class MyTreeView deriving from QTreeView.

        2. In that sub-class, override the expand slot (http://doc.qt.io/qt-5/qtreeview.html#expand). https://stackoverflow.com/questions/29170751/override-qt-slot-in-subclass may help you. You probably only need to do like https://stackoverflow.com/a/29172350/489865.

        3. Normally your override method would look like

        void MyTreeView::expand(const QModelIndex &index)
        {
            QTreeView::expand(index);
        }
        

        i.e. it's your job to call the base method to actually still do the expand if you override.

        Try commenting out the call to the base QTreeView::expand(index);. This should completely stop the tree view from actually expanding nodes, right? Then do whatever to check whether the index parameter refers to your current directory, and make it so in that case only it does not do the expand. So it ends up like

        void MyTreeView::expand(const QModelIndex &index)
        {
            if (isCurrentDirectoryIndex(index))
                return;
            QTreeView::expand(index);
        }
        

        Then you'll be done :)

        V 1 Reply Last reply
        1
        • JonBJ JonB

          @Vinoth-Rajendran4
          I don't do C++. You'll have to fill in details/corrections.

          1. Don't use a QTreeView directly if you are doing that at present. Create a sub-class MyTreeView deriving from QTreeView.

          2. In that sub-class, override the expand slot (http://doc.qt.io/qt-5/qtreeview.html#expand). https://stackoverflow.com/questions/29170751/override-qt-slot-in-subclass may help you. You probably only need to do like https://stackoverflow.com/a/29172350/489865.

          3. Normally your override method would look like

          void MyTreeView::expand(const QModelIndex &index)
          {
              QTreeView::expand(index);
          }
          

          i.e. it's your job to call the base method to actually still do the expand if you override.

          Try commenting out the call to the base QTreeView::expand(index);. This should completely stop the tree view from actually expanding nodes, right? Then do whatever to check whether the index parameter refers to your current directory, and make it so in that case only it does not do the expand. So it ends up like

          void MyTreeView::expand(const QModelIndex &index)
          {
              if (isCurrentDirectoryIndex(index))
                  return;
              QTreeView::expand(index);
          }
          

          Then you'll be done :)

          V Offline
          V Offline
          Vinoth Rajendran4
          wrote on last edited by
          #5

          @JonB : Since expand slot (http://doc.qt.io/qt-5/qtreeview.html#expand) isn't virtual , how i override it ??

          JonBJ 1 Reply Last reply
          0
          • V Vinoth Rajendran4

            @JonB : Since expand slot (http://doc.qt.io/qt-5/qtreeview.html#expand) isn't virtual , how i override it ??

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #6

            @Vinoth-Rajendran4
            As I said I'm not C++, so I'm afraid that is above my pay grade.

            If you can't do that, you might have to do something like intercept the clicks yourself, or can you "disable" an individual item, or maybe you can even derive from QFileSystemModel for your model and have that pretend that current directory has no children at all? These are just ideas....

            Edit Here's a thought: can you use http://doc.qt.io/qt-5/qfilesystemmodel.html#flags to clear the http://doc.qt.io/qt-5/qt.html Qt::ItemIsEnabled flag/setDisabled on the current directory in the model? Would that stop user expanding it?

            V 2 Replies Last reply
            2
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Hi
              There is also
              http://doc.qt.io/qt-5/qtreeview.html#itemsExpandable-prop

              JonBJ 1 Reply Last reply
              0
              • mrjjM mrjj

                Hi
                There is also
                http://doc.qt.io/qt-5/qtreeview.html#itemsExpandable-prop

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #8

                @mrjj There is, but that is tree-wide. OP only wants to not-expand one node....

                mrjjM 1 Reply Last reply
                1
                • JonBJ JonB

                  @mrjj There is, but that is tree-wide. OP only wants to not-expand one node....

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #9

                  @JonB
                  Ahh, i missed that. sounded like he wanted for all view.
                  TreeWidget has setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
                  so i assume there is a model alternative to that.
                  Im not sure if it actually disabled expanding or if its just cosmetic .

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    Vinoth Rajendran4
                    wrote on last edited by
                    #10
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Vinoth-Rajendran4
                      As I said I'm not C++, so I'm afraid that is above my pay grade.

                      If you can't do that, you might have to do something like intercept the clicks yourself, or can you "disable" an individual item, or maybe you can even derive from QFileSystemModel for your model and have that pretend that current directory has no children at all? These are just ideas....

                      Edit Here's a thought: can you use http://doc.qt.io/qt-5/qfilesystemmodel.html#flags to clear the http://doc.qt.io/qt-5/qt.html Qt::ItemIsEnabled flag/setDisabled on the current directory in the model? Would that stop user expanding it?

                      V Offline
                      V Offline
                      Vinoth Rajendran4
                      wrote on last edited by Vinoth Rajendran4
                      #11
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Vinoth-Rajendran4
                        As I said I'm not C++, so I'm afraid that is above my pay grade.

                        If you can't do that, you might have to do something like intercept the clicks yourself, or can you "disable" an individual item, or maybe you can even derive from QFileSystemModel for your model and have that pretend that current directory has no children at all? These are just ideas....

                        Edit Here's a thought: can you use http://doc.qt.io/qt-5/qfilesystemmodel.html#flags to clear the http://doc.qt.io/qt-5/qt.html Qt::ItemIsEnabled flag/setDisabled on the current directory in the model? Would that stop user expanding it?

                        V Offline
                        V Offline
                        Vinoth Rajendran4
                        wrote on last edited by
                        #12

                        @JonB said in How to control collapse/expand in QTreeView:

                        , or maybe you can even derive from QFileSystemModel for your model and have that pretend that current directory has no children at all?

                        found the solution with https://stackoverflow.com/a/18007243 , just as your idea :)

                        1 Reply Last reply
                        2

                        • Login

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