Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qt - widget - positioning

I want to place some widgets in a parent widget in some random places, like one button at Point (10,10) and another at (15,40), etc. How to achieve this?. QGridLayout is pushing everything into row column style. But I want to put the widgets whereever I want,Can anybody help me?

like image 427
prabhakaran Avatar asked Aug 31 '10 12:08

prabhakaran


2 Answers

If you really want to set absolute positions, I would ignore using a layout altogether. You can manually set the positions of elements by using the move() function or the setGeometry() function.

QWidget *parent = new QWidget();
parent->resize(400, 400);

QPushButton *buttonA = new QPushButton(parent);
buttonA->setText("First Button");
buttonA->move(10, 10);

QPushButton *buttonB = new QPushButton(parent);
buttonB->setText("Second Button");
buttonB->move(15, 40);

Side note: I would avoid setting absolute positions of elements in Qt. Why? Well, Qt tries to be a platform-independent GUI library. On different platforms, a lot of display things can change (i.e. font size of text in push buttons) so the size of your actual push buttons can vary to accommodate large or smaller font sizes. This can throw off your meticulously spaced push buttons is you use absolute positions as in the example above.

If you use layouts, overlapping buttons or buttons falling off the edge of your window can be avoided.

like image 68
Joel Verhagen Avatar answered Nov 05 '22 08:11

Joel Verhagen


You can see my answer for overlay button in QT: Qt Widget Overlays. This may help you to achieve what you want.

like image 24
Patrice Bernassola Avatar answered Nov 05 '22 10:11

Patrice Bernassola