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?


If you really want to set absolute positions, I would ignore using a layout all together. You can manually set the positions of elements by using 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.

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

Need Your Help

Read a file into an array using Perl

arrays perl file-io

I am currently reading a file and storing the data in an array named @lines. Then, I loop through this array using a for loop and inside the loop I match on certain values:

Retrieving the COM class factory for component with CLSID {XXXX} failed due to the following error: 80040154

.net windows-services com-interop 32bit-64bit

I developed a Windows service using C#.NET to generate PDF report. To generate PDF file I am using a third party dll. The application is running in my Windows XP platform. When I deployed the servi...