Qscrollarea not scrolling



  • 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);
    }@



  • 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);
    @



  • 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.



  • 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);
    @



  • 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.



  • 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.



  • 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
    

    @



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.