Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Alignment of layout doesn't work

    General and Desktop
    2
    2
    97
    Loading More Posts
    • 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
      bergungsdackel last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 1
        • First post
          Last post