Spacing in vertical layout
-
Hi
I have a scrollable area containing a Vertical layout.
I add items to the vertical layout but there appears to be a large amount of spacing between them as shown herehttps://www.dropbox.com/s/x718ndrmo123r1o/chart1.png
How can I get rid of this?
Here is the code for my main window
@
ui.setupUi(this);for (int s = 0; s < 3; s++)
{
QSplineSeries* series = new QSplineSeries();for (int i = 0; i < 100; ++i)
{
series->append(i,qSin(i));
}
QChart* chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);QChartView* chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->scale(1.0,0.5);
QHBoxLayout* hoizontalLayout = new QHBoxLayout(this);
QLabel *l1 = new QLabel(QString("%1").arg(s));
hoizontalLayout->addWidget(l1);hoizontalLayout->addWidget(chartView);
QLabel *l2 = new QLabel(QString("geo %1").arg(s));
hoizontalLayout->addWidget(l2);ui.verticalLayout->addLayout(hoizontalLayout);
}
@Thanks
-
Vertical and horizontal layouts always "spread" widgets evenly. If parent widget is 300px and you have 3 children widgets, all of them will end up with 100px. To "compact" just add horizontal or vertical spacer as last in layout. For vertical layout add vertical spacer, and for horizontal add horizontal spacer.
-
If i'm not wrong, a simple addStretch(1) after your last widget should push them to the top.
-
I have tried both of these but the result is still the same
@
ui.verticalLayout->addLayout(hoizontalLayout);
}ui.verticalLayout->addStretch(1);
// spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
//
// ui.verticalLayout->addItem(spacer);
@
Is there anything else I can try? -
Well the spacer item should've worked if you would have set the vertical sizepolicy to expanding. AFAIK the QSpacerItem consists of: width, height, hData(which is horizontal data), and vData(which is vertical data). So you should have for the spacer item this:
@spacer = new QSpacerItem(40, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);@
Also you should use addSpacerItem instead of addItem.