Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Layout doesn't align in the center
Qt 6.11 is out! See what's new in the release blog

Layout doesn't align in the center

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 4.6k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,
    I have a problem with my layouts. It seems they are aligning always to the right bottom corner instead of being equally spaced. I would like to have both layouts to have 3 pixels spacing to the borders and between each other which should be accomplished by how I chose the sizes of the Widgets. But instead it aligns them on the bottom right corner.
    As you can see I tried different ways of Qt::AlignCenter, to no avail.
    Can somebody please help me?
    This is my code for the mainwindow.cpp.

    @
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    m_QFrame(0)
    {
    setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.446, y1:0, x2:0.5, y2:1, stop:0 rgba(47, 149, 192, 255), stop:1 rgba(12, 87, 126, 255));");
    setFixedSize(800,480);
    QWidget *w = new QWidget( this );
    setCentralWidget(w);

    QVBoxLayout* mainLayout = new QVBoxLayout(w);
    
    // complete header containing HeaderLogo and overlay
    QFrame* headerFrame = new QFrame(this);
    headerFrame->setFixedHeight(60);
    headerFrame->setFixedWidth(800);
    headerFrame->setStyleSheet("background-color: rgba(255, 255, 255,50);");
    
    QFrame* overlay = new QFrame(headerFrame);
    overlay->setFixedHeight(50);
    overlay->setFixedWidth(671);
    overlay->setStyleSheet("background-color: rgba(255, 255, 255, 100); border-radius: 2px;");
    
    //Layout
    QHBoxLayout* headerFrameLayout = new QHBoxLayout(headerFrame);
    
    //headerLogo
    QLabel* headerLogo = new QLabel(headerFrame);
    headerLogo->setFixedSize(120,50);
    headerLogo->setStyleSheet("background-color: rgba(255, 255, 255, 100); border-radius: 2px;");
    
    headerFrameLayout->setAlignment(headerLogo,Qt::AlignCenter);
    headerFrameLayout->setAlignment(overlay,Qt::AlignCenter);
    headerFrameLayout->addWidget(headerLogo);
    headerFrameLayout->addWidget(overlay);
    headerFrame->setLayout(headerFrameLayout);
    
    QFrame* bodyFrame = new QFrame(this);
    bodyFrame->setFixedSize(790,411);
    bodyFrame->setStyleSheet("background-color: rgba(255, 255, 255,30); border-radius: 2px;");
    
    //header and body
    mainLayout->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(headerFrame);
    mainLayout->addWidget(bodyFrame);
    
    // Set the grid layout as a main layout
    w->setLayout(mainLayout);
    

    }@
    edit:
    I got a little bit closer to where I want to be: I added

    @ mainWidget->resize(800,480),
    setFixedSize(sizeHint());@

    so at least the mainLayout is aligning nicely now. But the Layout inside the headerframe still isn't...

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Charlie_Hdz
      wrote on last edited by
      #2

      If you're using Qt Creator, I suggest align your widgets by Qt Creator Designer.

      Qt Documentation might help you.

      Kind Regards,
      Enrique Hernandez
      gearstech.com.mx
      chernandez@gearstech.com.mx

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hi
        this post may be help you to resolve the problem http://qt-project.org/forums/viewthread/23292

        1 Reply Last reply
        0
        • mranger90M Offline
          mranger90M Offline
          mranger90
          wrote on last edited by
          #4

          The problem is the fixed height on headerFrame. The objects inside the layout aren't really at the bottom right, it just looks that way.

          1 Reply Last reply
          0

          • Login

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