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. QTreeWidget and QScrollArea
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget and QScrollArea

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 10.1k 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.
  • S Offline
    S Offline
    steno
    wrote on 16 Dec 2010, 21:20 last edited by
    #1

    Hi,

    I'm trying to create a QTreeWidget that does not allow for scrolling. Basically what I need is that the frame of the widget is the same size of the contents of the tree. So if you expand the tree, the frame should become bigger. By default, the frame stays the same and a scroll bar is added.

    (I would like to have this because I have a ScrollArea that contains one QTreeWidget with multiple other widgets in a QVBoxLayout. I would like 1 vertical scrollbar for the tree and all the other widgets at the same time, not one for the tree widget and one for the parent scroll area.)

    How would I go about doing this??

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on 16 Dec 2010, 21:26 last edited by
      #2

      You can disable scrolling by calling

      @
      QAbstractScrollArea::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      QAbstractScrollArea::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      @

      Then the scrollbars are gone. But you have to resize the view by yout own I think.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        steno
        wrote on 17 Dec 2010, 16:19 last edited by
        #3

        Thanx Gerolf, that idea worked. Had to connect to the signals itemExpanded and itemCollapsed to calculate the size. Here is an example of the recursive function to calculate the needed height.

        @
        void myTreeWidget::CalculateHeight()
        {
        int h = 0;

        int topLevelCount = topLevelItemCount();
        
        for(int i = 0;i < topLevelCount;i++)
        {
            QTreeWidgetItem * item = topLevelItem(i);
            h += CalculateHeightRec(item);
        }
        
        if(h != 0)
        {
            h += header()->sizeHint().height();
            setMinimumHeight(h);
        }
        

        }

        int myTreeWidget::CalculateHeightRec(QTreeWidgetItem * item)
        {
        if(!item)
        return 0;

        QModelIndex index = indexFromItem(item);
        
        if(!item->isExpanded())
        {
            return rowHeight(index);
        }
        
        int h = item->sizeHint(0).height() + 2;
        int childCount = item->childCount();
        for(int i = 0; i < childCount;i++)
        {
            h += CalculateHeightRec(item->child(i));
        }
        
        return h;
        

        }

        @

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on 18 Dec 2010, 23:57 last edited by
          #4

          If you set the scroll mode to QAbstractItemView::ScrollPerPixel (see "setHorizontalScrollMode() ":http://doc.qt.nokia.com/latest/qabstractitemview.html#horizontalScrollMode-prop for details) you might get the treeviews contents height (without the border and the headers) from "QScrollBar::maximum() ":http://doc.qt.nokia.com/latest/qabstractslider.html#maximum-prop. The scrollbar maxium is calculated even if the scrollbar policy "always off". That might save you some extra calculations.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mohsen
            wrote on 22 Sept 2012, 08:22 last edited by
            #5

            adding just a note: Volker's solution is more rational but not works. steno's did

            1 Reply Last reply
            0
            • E Offline
              E Offline
              EMStegehuis
              wrote on 21 Dec 2017, 09:54 last edited by
              #6

              I tried the solution of steno of calculating the height and setting the minimumHeight. My problem there is that the item-sizeHint (0).height() returns -1?

              Any idea why that is?

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 21 Dec 2017, 10:01 last edited by VRonin
                #7

                since Qt 5.2 this is much easier to do:
                view->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1
                • E Offline
                  E Offline
                  EMStegehuis
                  wrote on 21 Dec 2017, 10:12 last edited by
                  #8

                  That works perfect. Thanks a lot!

                  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