Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Alignment of layout doesn't work



  • Hello.

    I tried to create some buttons and labels in a layout (no matter which layout - vbox, hbox, gridlayout) but it doesn't align. Its position is everytime in the top left corner and I can't change it...

    Code from mainwindow.cpp

        QGridLayout *gLayout = new QGridLayout(this);
    
        QLabel *headText = new QLabel(this);
        headText->setText("Welcome to your point'n'click adventure");
        QPushButton *startButton = new QPushButton("&Start game",this);
    
        gLayout->setAlignment(Qt::AlignCenter);
        gLayout->addWidget(headText,0,0,Qt::AlignCenter);
        gLayout->addWidget(startButton,1,0,Qt::AlignCenter);
    
        this->setLayout(gLayout);
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt ?
    On what OS ?
    What is MainWindow ?
    Please provide a complete minimal compilable example that reproduce the behaviour so that people can test.

    On a side note, QGridLayout *gLayout = new QGridLayout(this); already applies the layout on the widget represented by this therefore this->setLayout(gLayout); is redundant. If MainWindow is a QMainWindow, then you can't apply a layout on it because it already has one that provides all the bells and whistles of that class.


Log in to reply