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.
    ...
    mainwin.h
    @class mainwin: public QWidget
    {
    Q_OBJECT
    private:
    QPushButton *Button1 , *Button2;
    QLabel *label;

    public:
    mainwin();
    ///.....
    }@
    mainwin.cpp
    @mainwin::mainwin()
    {
    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(Button1);
    layout->addWidget(Button2);
    layout->addWidget(label );
    this->setLayout(layout);
    }
    @
    main.cpp
    @int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    mainwin *mywindow=new mainwin();
    mywindow->show();
    return app.exec();
    }@



  • If you use "layouts":http://qt-project.org/doc/qt-5/layout.html then the geometry of the widgets and their location are defined by layout management.
    You can use "spacers":http://qt-project.org/doc/qt-5/qspaceritem.html 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.


  • Moderators

    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
 

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