Layout doesn't align in the center



  • 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...



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

    Qt Documentation might help you.



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



  • 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.


Log in to reply
 

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