Unsolved 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); scrollArea->setObjectName(QStringLiteral("scrollArea")); scrollArea->setWidgetResizable(true); scrollAreaWidgetContents = new QWidget(); scrollAreaWidgetContents->setObjectName(QStringLiteral("scrollAreaWidgetContents")); scrollAreaWidgetContents->setGeometry(QRect(0, 0, 347, 85)); scrollArea->setWidget(scrollAreaWidgetContents); 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); QScroller::scroller(scrollArea->viewport())->setScrollerProperties(ScrollerProperties); 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)); } else label_2[i]->setText(nachricht[ic]); ic++; } 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)); pos--; }
-
Hi,
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 onscrollAreaWidgetContents
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:
scrollAreaWidgetContents->resize(70,30*pos); scrollAreaWidgetContents->update(); scrollArea->setViewport(scrollAreaWidgetContents);
but i get still the same result -.-
-
@Allerknappe said in Add a Widget to QScrollArea:
i want to change the width of the label
Hi
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 :)