PDA

View Full Version : Resizing buttons in Layout



"BumbleBee"
24th March 2011, 12:28
So,I'm making a Tic Tac Toe game and I'd like to put my buttons,like: 3 rows of 3 buttons.
So far I have this:

win = new QWidget;
btn[0] = new QPushButton(this);
btn[1] = new QPushButton(this);
btn[2] = new QPushButton(this);
btn[3] = new QPushButton(this);
btn[4] = new QPushButton(this);
btn[5] = new QPushButton(this);
btn[6] = new QPushButton(this);
btn[7] = new QPushButton(this);
btn[8] = new QPushButton(this);
hl1 = new QHBoxLayout;
hl2 = new QHBoxLayout;
hl3 = new QHBoxLayout;
vl1 = new QVBoxLayout;

hl1->addWidget(btn[0]);
hl1->addWidget(btn[1]);
hl1->addWidget(btn[2]);

hl2->addWidget(btn[3]);
hl2->addWidget(btn[4]);
hl2->addWidget(btn[5]);

hl3->addWidget(btn[6]);
hl3->addWidget(btn[7]);
hl3->addWidget(btn[8]);

vl1->addLayout(hl1);
vl1->addLayout(hl2);
vl1->addLayout(hl3);


win->setLayout(vl1);
TicTacToe::setCentralWidget(win);

But buttons here have the normal size..how can I make then be square?

Thank you.

viulskiez
25th March 2011, 02:53
QButton default Horizontal Policy is Minimum and the Vertical Policy is Fixed. Try to change the sizePolicy property with QSizePolicy::Expanding or whatever you like, for example button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)

ChrisW67
25th March 2011, 04:10
A QGridLayout might be a good fit for this also.

"BumbleBee"
25th March 2011, 07:08
QButton default Horizontal Policy is Minimum and the Vertical Policy is Fixed. Try to change the sizePolicy property with QSizePolicy::Expanding or whatever you like, for example button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)


Yeah,thanks that worked.
But now what I have is a window with buttons all over it(I mean,when I resize window,the layout keeps growing...),but what I want,is to make the buttons on the one side(like make the layout ungrowable!) and some other widgets on the other side,outside the layout...

tomhanks
25th March 2011, 08:09
btn[0]->setGeometry(150,260,150,35);
...
setFixedSize(400, 400);

"BumbleBee"
25th March 2011, 08:30
This doesn't help me...it keeps resizing layout.

ChrisW67
25th March 2011, 09:48
Put the other stuff into your layout(s) and adjust the size policies, stretch factors and maximum sizes of the widgets to do sane things with extra space.

"BumbleBee"
25th March 2011, 11:57
U mean,to put the rests widgets to vl1?

ChrisW67
26th March 2011, 00:47
Yes, if that is the logical place for them. If not then you might need to nest your existing layout set into another horizontal box layout with more widgets to the right or left of the "grid" you have already.

"BumbleBee"
26th March 2011, 06:28
Ok,I did the 2nd.

Thanks