Add a Widget to QScrollArea

  • Hello,

    i want to add dynamicaly widgets to a QScrollArea.
    With my code i see the Labels, but can not scroll if a widget is outside of the visible scroll window.
    I thought first, its because the geometry settings are negativ, but also positive settings won´t work.
    Also i want to add the Widgets to an manual Layout.

    Maybe some help? Here my code(only the code for the scroll area) :

    QScrollArea *scrollArea;
    QWidget *scrollAreaWidgetContents;
    scrollArea = new QScrollArea(gridLayoutWidget);
    scrollAreaWidgetContents = new QWidget();
    scrollAreaWidgetContents->setGeometry(QRect(0, 0, 347, 85));
    QScroller::grabGesture(scrollArea->viewport(), QScroller::LeftMouseButtonGesture);
    QVariant OvershootPolicy = QVariant::fromValue<QScrollerProperties::OvershootPolicy>(QScrollerProperties::OvershootAlwaysOff);
    QScrollerProperties ScrollerProperties = QScroller::scroller(scrollArea->viewport())->scrollerProperties();
    ScrollerProperties.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, OvershootPolicy);
    ScrollerProperties.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, OvershootPolicy);
    for(qint16 i = 0, ic = 0; i < ia ; i++)	//this loop builds the new message area
    //      label_2->setGeometry(QRect(30, 30, 131, 41));	geometry of the received messages
    //		label_3->setGeometry(QRect(80, 80, 131, 41));	geometry of the own messages
            if(i => label_2.size())
                label_2.append(new QLabel(scrollAreaWidgetContents));
    			if(nachricht[ic].size() > 15)
    				sizeright = nachricht[ic].size() -15;
                    label_2[i]->setText(nachricht[ic].left(15)+ "/n"+nachricht[ic].right(sizeright));
    		label_2[i]->show();		// with show the label widget will be visible
    		label_2[i]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            label_2[i]->setGeometry(QRect(recx, 80-50*pos, 131, 41));

  • Lifetime Qt Champion


    Since you are not using any layout you have to also resize scrollAreaWidgetContents by hand. I'd recommend changing your current technique by using a QVBoxLayout that you set on scrollAreaWidgetContents and then append your labels to it.

    Depending on what you want to do, maybe a QListView with a custom model might be simpler. You may need a QStyledItemDelegate depending on what you want to show and how you want to show it.

  • ok SGaust thanks for the post.

    i tried to use not an layout, because i want to change the width of the label and it´s maybe smoother without layout...

    so i tried to resize the widget with:


    but i get still the same result -.-

  • Qt Champions 2017

    @Allerknappe said in Add a Widget to QScrollArea:

    i want to change the width of the label

    Just as note. With layouts you can always change minimumWidth and get that width
    so i would not say its smoother without layouts considering what else they offer :)

Log in to reply

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