change location of QWidgets using setGeometry

  • Hi,
    I want set locations of QWidgets like pushbuttons, Labels etc in a widget, but by using setGeometry, sometimes only size is changing and never change the locations.

    I am using 800x480 screen. I want to set pushbutton on right-bottom corner, but it always locate in left-middle regardless of coordinate in setGeometry.

    QPushButton *backBtn = new QPushButton;
    backBtn->setStyleSheet("QPushButton {background-color : lightblue; color : black;}");
    backBtn->setGeometry(QRect(600, 400, 120, 40));

    In my application I want work setGeometry must work properly.

    Please help me regarding it.

  • After initialization in constructor, I do setGeometry then only it will stay on their absolute coordinates.
    But there are other issues like if I will do setStyleSheet, setText after initialization for widgets, it will again disturb the absolute locations.

    Please let me know it.


  • If I do setStyleSheet or setText for any QLabel, all widgets positions are disturb.

  • Lifetime Qt Champion


    Out of curiosity, why are you not using a layout manager ?

  • Hi,
    Currently I am using QVBoxLayout and I have checked for other layout also. But all are rearranged after any setText or setStyleSheet.
    My requirement is that I have a background image, some points in that image I want to use as push buttons, so it should not be horizontal, vertical, grid etc its only random locations.

    I am using it as a page, so I used QStackedWidget to handle pages.

    Please tell me which layout is suitable for it or any other way to solve it.


  • Lifetime Qt Champion

    if you have something where your controls are randomly placed then you can't use a layout manager. However you should maybe take a look at the Graphics View frame work and maybe QML

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.