[ACCEPTED]-Which Qt Widget to scroll through widgets?-scroll
Accepted answer
Here is how I do it with a QVBoxLayout
and a QScrollArea
:
//scrollview so all items fit in window
QScrollArea* techScroll = new QScrollArea(tabWidget);
techScroll->setBackgroundRole(QPalette::Window);
techScroll->setFrameShadow(QFrame::Plain);
techScroll->setFrameShape(QFrame::NoFrame);
techScroll->setWidgetResizable(true);
//vertical box that contains all the checkboxes for the filters
QWidget* techArea = new QWidget(tabWidget);
techArea->setObjectName("techarea");
techArea->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
techArea->setLayout(new QVBoxLayout(techArea));
techScroll->setWidget(techArea);
Then 2 when adding items you do it like this (with 1 lay = techArea->layout()
and parent = techarea
:
for(std::set<Event::Enum>::iterator it = validEvents.begin(); it != validEvents.end();
++it){
QCheckBox* chk = new QCheckBox(
"text", parent);
if(lay){
lay->addWidget(chk);
}
}
If your display elements are simple, the 4 easiest solution is a QListWidget
. This will automatically 3 resize itself and inform the QScrollArea
when you add 2 items. You just have to call myScrollAlrea -> setWidget (myListWidget)
to initialise, and 1 then myListWidget -> addItem (myListWidgetItem)
to add new items.
RedX's answer was a bit vague, but I got 1 his method to work:
QRadioButton *radio[40];
for (int i = 0;i<40;i++)
radio[i] = new QRadioButton(tr("&Radio button 1"));
QWidget* techArea = new QWidget;
techArea->setObjectName("techarea");
techArea->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
techArea->setLayout(new QVBoxLayout(techArea));
ui->scrollArea->setWidget(techArea);
QLayout *lay = techArea->layout();
for (int i = 0;i<40;i++)
lay->addWidget(radio[i]);
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.