Important: Please read the Qt Code of Conduct -

Geometry of widget

  • Hi, I want to set the geometry of these widgets to define where there have to be displayed.. I would that label appear in the upper left and the buttons at bottom. I tried to test the geometry of label.. but it doesn't appear in the location I want.
    @class mainwin: public QWidget
    QPushButton *Button1 , *Button2;
    QLabel *label;

    Button1 = new QPushButton("Button1 ",this);
    Button2 = new QPushButton("Button2 ",this);
    label = new QLabel();
    label ->setStyleSheet("color: blue; background-color: red");
    label ->setGeometry(0,0,20,30); // ??

    QHBoxLayout *layout = new QHBoxLayout;

    layout->addWidget(label );
    @int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    mainwin *mywindow=new mainwin();
    return app.exec();

  • If you use "layouts": then the geometry of the widgets and their location are defined by layout management.
    You can use "spacers": to adjust the size and location.

  • Moderators

    setGeometry and layouts are mutually exclusive. Use setGeometry for static, non-resizable positioning and layouts for everything else.

    The layout you used is no good for this case. QHBoxLayout lays out everything from left to right.
    What you describe sounds more like a QGridLayout.
    Put label at [0,0], vertical spacer at [0,1], horizontal spacer at [0,2], and buttons at [1,2] and [2,2] grid cells.

  • Ok thanks.. I tried with grid layout and it's ok.

  • Hi,

    Could you edit your first post and prepend [solved]
    That way other users can see easily it's solved. Thanks!

Log in to reply