Reputation: 3
I want to have a dynamic list of QProgessBars
laid out vertically. But the thing is I want some of them to be moved horizontally. For example:
@@@@@@@@@@@@@@@@@
@@@@@@@@@@@
@@@@@@@@@@@
@@@@@@
When I move widgets in the layout like this:
sl->move(10, 0);
nothing happens.
I also tried using QGridLayout
with spacers:
QGridLayout * lay = new QGridLayout();
lay->setAlignment(Qt::AlignLeft|Qt::AlignTop);
for (int i = 1; i < 15; ++i) {
QProgressBar * sl = new QProgressBar();
QSpacerItem * sp = new QSpacerItem(10, 10 + i);
lay->addWidget(sl, i, 0);
lay->addItem(sp, i, 0);
}
I there a way to do it programmatically?
Upvotes: 0
Views: 489
Reputation: 14614
Do layout nesting, like they would do in html. Vertical layout with four (or three, if first row got only progress bar) horizontal layouts in it
But using Grid as top layout would allow to add margins at sides. Of course you can do that programmatically. I've shown how that may look in Designer, but what designer actually does is to generate plain linear code. You can do that in for cycle instead.
Generated code:
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(504, 377);
gridLayoutWidget = new QWidget(Dialog);
gridLayoutWidget->setObjectName(QString::fromUtf8("gridLayoutWidget"));
gridLayoutWidget->setGeometry(QRect(9, 9, 481, 351));
gridLayout = new QGridLayout(gridLayoutWidget);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setContentsMargins(0, 0, 0, 0);
horizontalLayout_3 = new QHBoxLayout();
horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_3->addItem(horizontalSpacer);
progressBar_2 = new QProgressBar(gridLayoutWidget);
progressBar_2->setObjectName(QString::fromUtf8("progressBar_2"));
progressBar_2->setValue(24);
horizontalLayout_3->addWidget(progressBar_2);
horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_3->addItem(horizontalSpacer_2);
gridLayout->addLayout(horizontalLayout_3, 1, 0, 1, 1);
horizontalLayout_2 = new QHBoxLayout();
horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_2->addItem(horizontalSpacer_3);
progressBar_3 = new QProgressBar(gridLayoutWidget);
progressBar_3->setObjectName(QString::fromUtf8("progressBar_3"));
progressBar_3->setValue(24);
horizontalLayout_2->addWidget(progressBar_3);
gridLayout->addLayout(horizontalLayout_2, 2, 0, 1, 1);
horizontalLayout = new QHBoxLayout();
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
progressBar_4 = new QProgressBar(gridLayoutWidget);
progressBar_4->setObjectName(QString::fromUtf8("progressBar_4"));
progressBar_4->setValue(24);
horizontalLayout->addWidget(progressBar_4);
horizontalSpacer_4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer_4);
gridLayout->addLayout(horizontalLayout, 3, 0, 1, 1);
horizontalLayout_4 = new QHBoxLayout();
horizontalLayout_4->setObjectName(QString::fromUtf8("horizontalLayout_4"));
progressBar = new QProgressBar(gridLayoutWidget);
progressBar->setObjectName(QString::fromUtf8("progressBar"));
progressBar->setValue(24);
horizontalLayout_4->addWidget(progressBar);
gridLayout->addLayout(horizontalLayout_4, 0, 0, 1, 1);
Upvotes: 0
Reputation: 150
Hi you need to use this QGridLayout::addWidget version. For instance, when you execute
grid->addWidget(w1, 0, 0, 1, 5);
grid->addWidget(w2, 1, 1, 2, 3);
grid->addWidget(w3, 3, 0, 1, 5);
your final gui will look something like this:
w1 w1 w1 w1 w1
w2 w2 w2
w2 w2 w2
w3 w3 w3 w3 w3
You can see the grid as a 2D-array. So w2 starts from grid[1][1]
and spans over 2 rows and 3 columns. And since w2 spans over 2 rows, w3 should start at w1's row + 2 (i.e. 3).
Upvotes: 3