PDA

View Full Version : Force all widgets in a QGridLayout to be visible



SeanM
29th January 2014, 20:36
I feel like this should be a simple layout question, but I'm having trouble getting it to work...

I've got a custom widget that inherits from QWidget and has a QGridLayout member that is set on this widget itself

myContainer::myContainer(QWidget* parent):QWidget(parent)
{
m_layout = new QGridLayout;
setLayout(m_layout);
}
I've got two types of widgets that go into the grid, so the grid is going to be an (n,2) grid (n rows, 2 columns). As the application is resized, I want the height of the widgets in my grid to be roughly (1/N * height of myContainer). So basically I want the application's layouts to figure out what how many pixels to give myContainer, and once that's figured out, then resize all the rows so that all the rows are visible. Instead it seems to be working from the inside out, where the items in the rows are getting some default height, which then pushes the height of myContainer to be N * that height, which then makes the application taller than the height of an actual monitor which means some of the rows aren't visible.

I've tried setting the vertical size policies of all my widgets to QSizePolicy::Maximum, but that doesn't seem to get it to work correctly.

ChrisW67
29th January 2014, 21:29
You are describing the default behaviour of the grid layout if all the widgets in it have no minimum size constraint and the same stretch factors (see example). Perhaps you need to tell us what the widget are, how you are adding them etc.


#include <QtGui>

class Container: public QWidget {
public:
explicit Container(QWidget *p = 0): QWidget(p) {
QGridLayout *l = new QGridLayout(this);
for (int r = 0; r < 5; ++r) {
for (int c = 0; c < 5; ++c) {
QFrame *frame = new QFrame(this);
frame->setFrameStyle(QFrame::Box | QFrame::Plain);
l->addWidget(frame, r, c);
}
}
}
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
Container c;
c.resize(640, 480);
c.show();
Container b;
b.resize(800, 600);
b.show();
return app.exec();
}

SeanM
29th January 2014, 22:07
I've removed my container widget, which wasn't really doing anything beyond wrapping a QGridLayout inside a widget anyways.

So now, the parent widget is a QTabWidget, my two widgets that I'm putting in the grid are a QwtPlot (col 0) and a QwtLegend (col 1). I've included the relevant code below.

QGridLayout* grid = new QGridLayout();
ui->tab_2->setLayout(grid);

int i=0;
foreach (QwtPlot* plot, plots)
{
plot->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
grid->addWidget(plot,i,0);
QwtLegend* legend = new QwtLegend();
legend->setMaxColumns(1);
legend->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
grid->addWidget(legend, i, 1);
i++;
}

This code is in my MainWindow constructor, and when the application launches, it takes up more vertical space than I'd expect it to. In the Qt Creator Designer tab, my MainWindow class has a geometry of 1158x738. When the application launches, the MainWindow size is 1158 x 1997...