2
votes

i'm having a problem with layouts in Qt. I'm trying to compile this code, with 2 horizontal layouts and a vertical main layout. Each horizontal layout have 3 buttons and both horizontal layout are incorporated in the vertical layout. But after i compile this code, i can only see a small window with just the "Exit" button.

firstline = new QHBoxLayout(this);
secondline = new QHBoxLayout(this);

layout = new QVBoxLayout(this);

eat = new QPushButton("Eat", this);
drink = new QPushButton("Drink", this);
smoke = new QPushButton("Smoke", this);

save = new QPushButton("Save", this);
load = new QPushButton("Load", this);
exit = new QPushButton("Exit", this);

firstline->addWidget(eat);
firstline->addWidget(drink);
firstline->addWidget(smoke);

secondline->addWidget(save);
secondline->addWidget(load);
secondline->addWidget(exit);

layout->addLayout(firstline);
layout->addLayout(secondline);

setLayout(layout);    
1
Remember there's QGridLayout that probably is simpler to use in this case.rubenvb
I know, but layouts just doens't work. I rewrited this code using QGridLayout and is just the same result. The problem must be mine or i'm missing something in code.Kanghu

1 Answers

4
votes

You are already setting the layout for your dialog through these statements...

 firstline = new QHBoxLayout(this);
 secondline = new QHBoxLayout(this);

So call their constructors without specifying their parent widget.

firstline = new QHBoxLayout();
secondline = new QHBoxLayout();

This would display your layout as you are expecting.