Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Alignment of layout doesn't work
Qt 6.12 LTS is out! See what's new in the release blog

Alignment of layout doesn't work

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 404 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bergungsdackel
    wrote on last edited by
    #1

    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);
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved