c++qtqlayout

Qt layout content is not visible


In this project a QVBoxLayout's content is invisible, I tried every single things but nothing changed. The layout's name is topLeftLayout. BottomLeftLayout and right layout are correctly visibles on the screen. I've no idea. This is the code:

.cpp

#include "math.h"

Math::Math(QWidget* parent) : QWidget(0)
{
    mainWidget = new QWidget();

    titleLabel = new QLabel(mainWidget);
    titleLabel->setText("Math Helper v. 0.0.0");

    bottomLabel = new QLabel(mainWidget);
    bottomLabel->setText("Made with C++ and Qt Framework");

    aboutQtButton = new QPushButton(mainWidget);
    aboutQtButton->setText("About Qt");
    
    quitButton = new QPushButton(mainWidget);
    quitButton->setText("Quit");

    // ????
    topLeftLayout = new QVBoxLayout(mainWidget);
    topLeftLayout->addWidget(titleLabel);
    topLeftLayout->addWidget(bottomLabel);
    topLeftLayout->addStretch();

    bottomLeftLayout = new QHBoxLayout(mainWidget);
    bottomLeftLayout->addWidget(aboutQtButton);
    bottomLeftLayout->addWidget(quitButton);
    bottomLeftLayout->addStretch();

    leftLayout = new QVBoxLayout(mainWidget);
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addLayout(bottomLeftLayout);

    rightLayout = new QVBoxLayout(mainWidget);

    mainLayout = new QHBoxLayout(mainWidget);
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);

    setLayout(mainLayout);
    setWindowTitle("Math Helper");
    setFixedSize(windowWidth, windowHeigth);
}
 

Solution

  • You assign several layouts to your widget. From the documentation QLayout::QLayout(QWidget *parent): The layout is set directly as the top-level layout for parent. There can be only one top-level layout for a widget.

    Untested:

    #include "math.h"
    
    Math::Math(QWidget* parent) : QWidget(parent)
    {
        //mainWidget = new QWidget();
    
        titleLabel = new QLabel;
        titleLabel->setText("Math Helper v. 0.0.0");
    
        bottomLabel = new QLabel;
        bottomLabel->setText("Made with C++ and Qt Framework");
    
        aboutQtButton = new QPushButton;
        aboutQtButton->setText("About Qt");
        
        quitButton = new QPushButton;
        quitButton->setText("Quit");
    
        // ????
        topLeftLayout = new QVBoxLayout;
        topLeftLayout->addWidget(titleLabel);
        topLeftLayout->addWidget(bottomLabel);
        topLeftLayout->addStretch();
    
        bottomLeftLayout = new QHBoxLayout;
        bottomLeftLayout->addWidget(aboutQtButton);
        bottomLeftLayout->addWidget(quitButton);
        bottomLeftLayout->addStretch();
    
        leftLayout = new QVBoxLayout;
        leftLayout->addLayout(topLeftLayout);
        leftLayout->addLayout(bottomLeftLayout);
    
        rightLayout = new QVBoxLayout;
    
        mainLayout = new QHBoxLayout;
        mainLayout->addLayout(leftLayout);
        mainLayout->addLayout(rightLayout);
    
        setLayout(mainLayout);
        setWindowTitle("Math Helper");
        //setFixedSize(windowWidth, windowHeigth);
    }