0

I'm new to qt and exploring it .Basically Im adding three child layouts to a Parent layout.But after having added the third layout,my first layout disappears. This is my code:

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow)
{

this->setGeometry(500,650,1000,1000);

QVBoxLayout *parentLayout = new QVBoxLayout(this);

QVBoxLayout *l1 = new QVBoxLayout();
QWidget *lw1 = new QWidget;
lw1->setMaximumHeight(50);
lw1->setStyleSheet("background-color:brown");
l1->addWidget(lw1);

QHBoxLayout *l2 = new QHBoxLayout;

QLabel *label = new QLabel("Industry");
label->setStyleSheet("color:white");
label->setMaximumWidth(300);
label->setAlignment(Qt::AlignTop);
l2->addWidget(label);


QComboBox *cb = new QComboBox;
cb->addItem("Movie");
cb->setStyleSheet("background-color:white");
cb->setMaximumHeight(50);
l2->addWidget(cb);

l2->setAlignment(Qt::AlignTop);

parentLayout->addLayout(l1);

parentLayout->addLayout(l2);

//If I run till here I see the added two child layouts.

QWidget *w3 = new QWidget;
w3->setStyleSheet("background-color:white");

QVBoxLayout *l3 = new QVBoxLayout();
l3->addWidget(w3);
l3->setAlignment(Qt::AlignTop);


//parentLayout->addLayout(l3);
//If I uncomment the above line then I see only layouts l2 and l3 from the top and not l1

}

Basically what I need is a Layout which in turn containing multiple subLayouts of varied sizes.Could someone highlight me where am I doing mistake.Any help will be really useful.

george
  • 339
  • 3
  • 12
  • 1) Remove all `setAlignment(Qt::AlignTop)` calls for layouts, 2) Construct child layouts 3) Construct parent layout as child of the parent widget 4) Add child layouts in the order of their appearance in the parent layout. – vahancho Aug 10 '18 at 07:36
  • Thanks for the reply @vahancho.I tried 1) and 4) is already in place.2) I have defined l1,l2,l3 as shown above.3)Here my MainWindow is a QWidget and not QMainWindow ,so Im passing this directly to the constructor of parentLayout. – george Aug 10 '18 at 07:41
  • Your code looks more or less ok, but setting layouts alignment looks peculiar to me. You set both `l2` and `l3` alignment to top. Try to remove that calls. – vahancho Aug 10 '18 at 07:45
  • I have tried the code once with all those alignment calls removed.Stills l1 does'nt show up when l3 is added to ParentLayout! – george Aug 10 '18 at 07:51
  • As `lw1` is a bare `QWidget` it probably has a minimum size hint of (0, 0) so I suspect it *is* still there but with a height of zero pixels. Try [`lw1->setMinimumHeight(50)`](http://doc.qt.io/qt-5/qwidget.html#minimumHeight-prop) or similar to see if that helps. – G.M. Aug 10 '18 at 08:21
  • @GM Thanks man.I have added both lw1->setMinimumHeight and lw1->setMaximumHeight, and it works now.But why does these two functions exist and when should they be used?? – george Aug 10 '18 at 10:10
  • Like all functions they have their uses -- albeit few and far between. I would, however, echo the comments of @vahancho in that a lot of the problems people have with layouts in general are caused by trying to over-constrain things. 99% of the time widgets and layouts will just \`\`do the right thing'' without explicitly setting size, size policy or alignment etc. So start by just getting things visible before trying to finesse anything. – G.M. Aug 10 '18 at 12:06

0 Answers0