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. Qscrollarea not scrolling
Forum Updated to NodeBB v4.3 + New Features

Qscrollarea not scrolling

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 11.6k 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.
  • B Offline
    B Offline
    bigrockcrasher
    wrote on last edited by
    #1

    I have spent two days trying to get a scrollarea to scroll. So currently I am adding 47 labels to the VBox. Have I done something wrong??

    @
    QScrollArea *scrollArea = new QScrollArea();
    widget = new QWidget(scrollArea);
    VBox = new QVBoxLayout(widget);

    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scrollArea->setWidgetResizable(true);
    Info->addWidget(scrollArea); //Info is a -QHBoxLayout
    int s = Qair.size();
    for (int i = 0; i<s;i++) {
    label = new QLabel;
    label->setText(Qair[i]->Form);
    label->setMinimumHeight(20); //prevents labels from cutting each other off
    label->setMinimumWidth(Qair[i]->Form.length()*15); //attempt to get Scrollarea to allow horizontal scrolling
    VBox->addWidget(label);
    }@

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      I use the QScrollArea in some projects. I found it works quite well but I use a different approach.

      I create a parent widget and this is put into the scroll area. All the children use the parent widget and layout (d_scroll_area_widget, d_scroll_area_layout) to define where they sit. I use a vertical layout but a grid layout would work just the same.

      The spacer widget at the bottom of my example keeps the children from resizing to fill the parent widget. In my case the widget containing the scroll area is fixed so I put the spacer inside the scroll area. For me, the spacer solves a problem when you don't have enough children to fill the area. I dynamically move this to the bottom of all the children that are added as they are added.

      In your example the sizeHints might be the problem. Maybe the scroll area doesn't realize the size of the widget therefore it doesn't scroll (?)

      @
      QVBoxLayout *widget_vlayout;
      QScrollArea *scroll_area;

      widget_vlayout = new QVBoxLayout(this); // layout for parent containing scroll area

      scroll_area = new QScrollArea(this);
      scroll_area->setWidgetResizable(true);
      widget_vlayout->addWidget(scroll_area);

      d_scroll_area_widget = new QWidget();
      scroll_area->setWidget(d_scroll_area_widget);

      d_scroll_area_layout = new QVBoxLayout(d_scroll_area_widget);

      d_bottom_vertical_spacer = new QSpacerItem(100, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
      d_scroll_area_layout->addItem(d_bottom_vertical_spacer);
      @

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bigrockcrasher
        wrote on last edited by
        #3

        So looking at your code it looks like the only difference is adding a QSizePolicy. I implemented that all it did was add a space at the top.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

          No. The QSizePolicy is part of the constructor for the spacer item (text is wrapped around so it looks like a new line).

          One thing that might be the problem is the parent of the 'widget'. The parent of the widget is the scroll area which has no parent. If you reverse this it might work.

          @
          QScrollArea *scrollArea = new QScrollArea();
          widget = new QWidget(scrollArea);
          VBox = new QVBoxLayout(widget);
          @

          to
          @
          QScrollArea *scrollArea = new QScrollArea(this); // assuming dialog or widget
          widget = new QWidget();
          VBox = new QVBoxLayout(widget);
          @

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bigrockcrasher
            wrote on last edited by
            #5

            Ok your correct, did not notice that. I reversed it. i did not mention that the scrollarea is inside of a QHBoxLayout. but I did try using "this" as the parent widget. what I found is that if i did not set the scrollArea as the parent widget of VBox parent widget it would not display anything in the scroll area. I also created a separate widget for the scrollarea and nothing changes.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bigrockcrasher
              wrote on last edited by
              #6

              Ok i got it fixed

              scrollArea does not get a parent widget. I must use scrollArea->setwidget(parent widget of Vbox). parent widget of Vbox does not require a parent widget as i menstioned in my last post.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rondog
                wrote on last edited by
                #7

                The scrollArea inside a QHBoxLayout is fine. I use the same idea

                You should not need to set the parent of 'widget' to the scrollArea. I don't know if this is the same as the function 'scrollArea->setWidget' but if this was changed a bit more it would be virtually identical to my example:

                @
                QScrollArea *scrollArea = new QScrollArea(this);
                widget = new QWidget();
                VBox = new QVBoxLayout(widget);
                ....
                scrollArea->setWidget(widget);
                ....
                for (int i = 0; i<s;i++) {
                label = new QLabel(widget);
                ...
                VBox->addWidget(label);
                }

                widget->setVisible(true); // if not inside constructor
                

                @

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rondog
                  wrote on last edited by
                  #8

                  Okay. Good news.

                  I would probably set the parent of QScrollArea and any of the labels. If you don't they won't be (may not be) deleted when the parent is deleted.

                  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