PDA

View Full Version : How can I programmatically add QPushButtons in a QFrame without using Layout?



binary001
15th December 2015, 13:52
Hi,

I add qpushbuttons on Form and set my buttons' positions with setGeometry. It's ok.
Now I want to divide these buttons into QFrame as the groups.

I add buttons on Form as follow:
myBtn[btn]=new QPushButton(this);
myBtn[btn]->setGeometry(intWidth*i,y,intWidth,intWidth);

How can I add these buttons into QFrame? VBoxLayout, HBoxLayout or GridLayout is not suitable for my buttons' position.
So I don't want to use layout.

Thanks.

Vikram.Saralaya
15th December 2015, 14:27
Make your push buttons child of QFrame.


QFrame frame = new QFrame(this);
myBtn[btn]=new QPushButton(frame);
myBtn[btn]->setGeometry(intWidth*i,y,intWidth,intWidth);

Now setGeometry will set the push button positions with respect to its parent QFrame.

anda_skoa
15th December 2015, 14:29
Not using a layout is usually a bad idea. Are you having such a dynamic situation that you need to move the buttons a lot?

In any case, I don't quite understand your problem.
Layouts don't add widgets to frames, they manage the size and location within the parent widget, whether that is a frame or some other widget.

Cheers,
_

binary001
15th December 2015, 16:32
Thanks for your quick replay.

Now I save my button locations and sizes in struct array with map number.
I place freely these buttons with maps without any layout.
QFrame frame = new QFrame(this);
myBtn[btn]=new QPushButton(frame);
myBtn[btn]->setGeometry(intWidth*i,y,intWidth,intWidth);
It's OK.

Thanks anda_skoa for your suggestion.
I also think "Not using a layout is usually a bad idea". But I can't decide which type of layout should I use to add there buttons on the Form according to my maps?

11577

Please see picture of my button position and size. It's a map number 1.
There are many maps to place buttons. Number of buttons and locations are different for maps.
Thanks

d_stranz
15th December 2015, 18:02
Please see picture of my button position and size. It's a map number 1.
There are many maps to place buttons. Number of buttons and locations are different for maps.

What is a "map"? Is it a button or is it a container (widget) where you want to place one or more buttons?

binary001
16th December 2015, 04:58
I want to add 7 buttons (please see above my post). The buttons' positions and sizes are shown at picture ( I named a group of buttons' sizes and positions as a map).
Which layout can I use to place buttons on a Form?

Thanks.

Vikram.Saralaya
16th December 2015, 11:55
You should be using a QGridLayout.
Especially rowSpan and columnSpan with the alignment should be sufficient to get the widgets as shown in your image.


void QGridLayout::addWidget(QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0)