Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

Grid of QGridLayout not respected



  • Can you help me to align my widgets at grid?

    2021-06-16 11_00_05-Window.png

    SettingsMenu::SettingsMenu(QWidget *parent) : QFrame(parent)
    {
    
        this->setFrameShape(QFrame::StyledPanel);
        this->setFrameShadow(QFrame::Plain);
    
        QGridLayout* layout = new QGridLayout(this);
    
        makeLanguagesButton();
        QLabel* langLbl = new QLabel("Languages");
        langLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeClockButton();
        QLabel* clockLbl = new QLabel("Clock");
        clockLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeInfoButton();
        QLabel* infoLbl = new QLabel("Info");
        infoLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeCodeButton();
        QLabel* codeLbl = new QLabel("Password");
        codeLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeWifiSettingButton();
        QLabel* wifiLbl = new QLabel("WiFi");
        wifiLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeNotificationButton();
        QLabel* notificationLbl = new QLabel("Notifications");
        notificationLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        QVBoxLayout* langLayout = new QVBoxLayout();
        langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(langLbl,      0, Qt::AlignCenter);
    
        QVBoxLayout* clockLayout = new QVBoxLayout();
        langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
    
        QVBoxLayout* infoLayout = new QVBoxLayout();
        langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
    
        QVBoxLayout* codeLayout = new QVBoxLayout();
        langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
    
        QVBoxLayout* wifiLayout = new QVBoxLayout();
        langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
    
        QVBoxLayout* notificationLayout = new QVBoxLayout();
        langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
    
        layout->addLayout(langLayout,          0, 0, Qt::AlignCenter);
        layout->addLayout(clockLayout,         0, 1, Qt::AlignCenter);
        layout->addLayout(infoLayout,          0, 2, Qt::AlignCenter);
        layout->addLayout(codeLayout,          1, 0, Qt::AlignCenter);
        layout->addLayout(wifiLayout,          1, 1, Qt::AlignCenter);
        layout->addLayout(notificationLayout,  1, 2, Qt::AlignCenter);
    
        layout->setContentsMargins(5,5,5,5);
    
        this->setLayout(layout);
    }
    

  • Lifetime Qt Champion

    Hi
    But why all the sub layouts ?

    is the goal just to have them in a grid like ?

    alt text



  • I need the sub layouts because inside every cell of grid I need a pixmap upper and a label below.

    This is the goal

    2021-06-16 11_00_05-Window.png



  • @Simof said in Grid of QGridLayout not respected:

    QVBoxLayout* langLayout = new QVBoxLayout();
    langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(langLbl, 0, Qt::AlignCenter);

    QVBoxLayout* clockLayout = new QVBoxLayout();
    langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
    
    QVBoxLayout* infoLayout = new QVBoxLayout();
    langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
    
    QVBoxLayout* codeLayout = new QVBoxLayout();
    langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
    
    QVBoxLayout* wifiLayout = new QVBoxLayout();
    langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
    
    QVBoxLayout* notificationLayout = new QVBoxLayout();
    langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
    

    If this is your real code, you made a simple mistake :)

    Check your layout names... You add ALL your pairs (pixmap + label) to langLayout, but you create one for each of them. Then you add all to your grid. This results in all your content being your first row/column i.e. langLayout while the other layouts in your grid stay empty

    QVBoxLayout* codeLayout = new QVBoxLayout();
    langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);

    Replace

    QVBoxLayout* codeLayout = new QVBoxLayout();
    langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
    

    for example with:

    QVBoxLayout* codeLayout = new QVBoxLayout();
    codeLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
    codeLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
    


  • @Pl45m4 said in Grid of QGridLayout not respected:

    @Simof said in Grid of QGridLayout not respected:

    QVBoxLayout* langLayout = new QVBoxLayout();
    langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(langLbl, 0, Qt::AlignCenter);

    QVBoxLayout* clockLayout = new QVBoxLayout();
    langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
    
    QVBoxLayout* infoLayout = new QVBoxLayout();
    langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
    
    QVBoxLayout* codeLayout = new QVBoxLayout();
    langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
    
    QVBoxLayout* wifiLayout = new QVBoxLayout();
    langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
    
    QVBoxLayout* notificationLayout = new QVBoxLayout();
    langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
    

    If this is your real code, you made a simple mistake :)

    Check your layout names... You add ALL your pairs (pixmap + label) to langLayout, but you create one for each of them. Then you add all to your grid. This results in all your content being your first row/column i.e. langLayout while the other layouts in your grid stay empty

    QVBoxLayout* codeLayout = new QVBoxLayout();
    langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);

    Replace

    QVBoxLayout* codeLayout = new QVBoxLayout();
    langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
    langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
    

    for example with:

    QVBoxLayout* codeLayout = new QVBoxLayout();
    codeLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
    codeLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
    

    Yes this is my real code! Thank a lot, I lose a lot of time and it was a very stupid mistake.



  • @Simof said in Grid of QGridLayout not respected:

    I lose a lot of time and it was a very stupid mistake.

    This happens very often if you copy & paste lines of code, because you need it multiple times. Then, when you forget to change the var names correctly you run into stuff like this. And most of the time you don't know what and why this happend :)


Log in to reply