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
QtWS25 Last Chance

How to control collapse/expand in QTreeView

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 11.2k Views
  • 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 Offline
    V Offline
    Vinoth Rajendran4
    wrote on 13 Sept 2018, 12:14 last edited by Vinoth Rajendran4
    #1

    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!

    J 1 Reply Last reply 13 Sept 2018, 12:21
    0
    • V Vinoth Rajendran4
      13 Sept 2018, 12:14

      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!

      J Offline
      J Offline
      JonB
      wrote on 13 Sept 2018, 12:21 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 13 Sept 2018, 16:50
      0
      • J JonB
        13 Sept 2018, 12:21

        @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 13 Sept 2018, 16:50 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.

        J 1 Reply Last reply 13 Sept 2018, 17:32
        0
        • V Vinoth Rajendran4
          13 Sept 2018, 16:50

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

          J Offline
          J Offline
          JonB
          wrote on 13 Sept 2018, 17:32 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 14 Sept 2018, 12:18
          1
          • J JonB
            13 Sept 2018, 17:32

            @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 14 Sept 2018, 12:18 last edited by
            #5

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

            J 1 Reply Last reply 14 Sept 2018, 13:34
            0
            • V Vinoth Rajendran4
              14 Sept 2018, 12:18

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

              J Offline
              J Offline
              JonB
              wrote on 14 Sept 2018, 13:34 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 16 Sept 2018, 14:21
              2
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 14 Sept 2018, 13:43 last edited by
                #7

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

                J 1 Reply Last reply 14 Sept 2018, 13:49
                0
                • M mrjj
                  14 Sept 2018, 13:43

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 14 Sept 2018, 13:49 last edited by
                  #8

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

                  M 1 Reply Last reply 14 Sept 2018, 13:55
                  1
                  • J JonB
                    14 Sept 2018, 13:49

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

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 14 Sept 2018, 13:55 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 16 Sept 2018, 14:19 last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • J JonB
                        14 Sept 2018, 13:34

                        @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 16 Sept 2018, 14:21 last edited by Vinoth Rajendran4
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • J JonB
                          14 Sept 2018, 13:34

                          @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 16 Sept 2018, 14:24 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

                          5/12

                          14 Sept 2018, 12:18

                          • Login

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