2

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);    
Kanghu
  • 561
  • 1
  • 10
  • 23
  • Remember there's [`QGridLayout`](http://qt-project.org/doc/qt-4.8/QGridLayout.html) that probably is simpler to use in this case. – rubenvb May 15 '12 at 09:40
  • 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 May 15 '12 at 10:12

1 Answers1

4

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.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
ScarCode
  • 3,074
  • 3
  • 19
  • 32
  • @Mat The parentheses in the new calls have a very stinky difference: http://stackoverflow.com/a/620402/256138. Best to just include them. – rubenvb May 15 '12 at 09:53
  • @rubenvb: I disagree. Theses are clearly non-PODs and all the Qt documentation is written without the parens. – Mat May 15 '12 at 10:01
  • It still doesn't work properly. Same result after letting the constructor blank. – Kanghu May 15 '12 at 10:11
  • Make sure that you don't have any layout added via Qt IDE.It worked fine on my system – ScarCode May 15 '12 at 10:35
  • Oh, i figured the problem and solved it. I was using QMainWindow as the window widget instead of using QWidget. Thank you for helping me. – Kanghu May 15 '12 at 10:47