QHBoxLayout spacing changes when adding QWidget instead of QTextEdit

44 Views Asked by At

I have the following code that gives me Lineedits (see picture):

QHBoxLayout *mainQHBoxLayout = new QHBoxLayout(); 
QWidget *trayQWidget = new QWidget();
trayQWidget->setLayout(mainQHBoxLayout);

ui.centralWidget->layout()->addWidget(trayQWidget);
for (int lauf = 10;lauf < 15;lauf++)
{
    QTextEdit *edit = new QTextEdit;
    edit->setText(QString("edit %1").arg(lauf));
    edit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    edit->setMaximumWidth(8 * lauf);
    edit->setMaximumHeight(7 * lauf);
    edit->setEnabled(false);
    edit->setAlignment(Qt::AlignCenter);
    edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);     
    mainQHBoxLayout->addWidget(edit);
    mainQHBoxLayout->setAlignment(mainQHBoxLayout,Qt::AlignBottom);
}
mainQHBoxLayout->setSpacing(2);
mainQHBoxLayout->addStretch();

enter image description here

I am wondering why the Lineedits are not aligned to the bottom?

To achive the alignment i tried to add aditional QVBoxLayouts. Now the Allignemt ist OK but the spacing between the QlineEdits ist too big. (See picture)

QHBoxLayout *mainQHBoxLayout = new QHBoxLayout(); 
QWidget *trayQWidget = new QWidget();
trayQWidget->setLayout(mainQHBoxLayout);

ui.centralWidget->layout()->addWidget(trayQWidget);
for (int lauf = 10;lauf < 15;lauf++)
{
    QVBoxLayout *myQVBoxLayout = new QVBoxLayout();
    QWidget *itemQWidget = new QWidget();
    itemQWidget->setLayout(myQVBoxLayout);
    myQVBoxLayout->addStretch();
    
    QTextEdit *edit = new QTextEdit;
    edit->setText(QString("edit %1").arg(lauf));
    edit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    edit->setMaximumWidth(8 * lauf);
    edit->setMaximumHeight(7 * lauf);
    edit->setEnabled(false);
    edit->setAlignment(Qt::AlignCenter);
    edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);     
    myQVBoxLayout->addWidget(edit);
    mainQHBoxLayout->addWidget(itemQWidget);
}
mainQHBoxLayout->setSpacing(2);
mainQHBoxLayout->addStretch();

enter image description here

Has anybody an suggestion what could be my fault or what i could try next?

Framework is QT 5.6.1 Platform Windows10

2

There are 2 best solutions below

0
On

I think the solution ist to set the Margin of the myQVBoxLayout to "0"

myQVBoxLayout->setMargin(0);
1
On

First code snippet:

This line is wrong:

mainQHBoxLayout->setAlignment(mainQHBoxLayout,Qt::AlignBottom);

Does it not look strange to you to have mainQHBoxLayout used as the parameter of a method called upon itself, several times at that (once per loop since unlike edit, mainQHBoxLayout stays constant throughout your widget initialization)?
setAlignment is to align a child widget or a child layout in this fashion:

mainQHBoxLayout->setAlignment(edit, Qt::AlignBottom);

Second code snippet:

A layout inside a widget has margins by default, therefore you need to get rid of them like so:

myQVBoxLayout->setContentsMargins(QMargins());

Better yet, get rid of itemQWidget and add each of your myQVBoxLayout generated in the loop as child layouts to your main layout. That way, no margins are created by default.

mainQHBoxLayout->addLayout(myQVBoxLayout);

The below code shows all 3 methods as reproducible example:

#include <QtWidgets/QApplication>
#include <QtWidgets/QLayout>
#include <QtWidgets/QTextEdit>


int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    { //Correctly align  widgets
        QHBoxLayout* mainQHBoxLayout = new QHBoxLayout();
        QWidget* trayQWidget = new QWidget();
        trayQWidget->setLayout(mainQHBoxLayout);

        for (int lauf = 10; lauf < 15; lauf++)
        {
            QTextEdit* edit = new QTextEdit;
            edit->setText(QString("Aligned widgets %1").arg(lauf));
            edit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
            edit->setMaximumWidth(8 * lauf);
            edit->setMaximumHeight(7 * lauf);
            edit->setEnabled(false);
            edit->setAlignment(Qt::AlignCenter);
            edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            mainQHBoxLayout->addWidget(edit);
            mainQHBoxLayout->setAlignment(edit, Qt::AlignBottom);
        }
        mainQHBoxLayout->setSpacing(2);
        mainQHBoxLayout->addStretch();
        trayQWidget->show();
    }

    { //Get rid of margins
        QHBoxLayout* mainQHBoxLayout = new QHBoxLayout();
        QWidget* trayQWidget = new QWidget();
        trayQWidget->setLayout(mainQHBoxLayout);

        for (int lauf = 10; lauf < 15; lauf++)
        {
            QVBoxLayout* myQVBoxLayout = new QVBoxLayout();
            myQVBoxLayout->setContentsMargins(QMargins());

            QWidget* itemQWidget = new QWidget();
            itemQWidget->setLayout(myQVBoxLayout);
            myQVBoxLayout->addStretch();

            QTextEdit* edit = new QTextEdit;
            edit->setText(QString("Widgets without margins %1").arg(lauf));
            edit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
            edit->setMaximumWidth(8 * lauf);
            edit->setMaximumHeight(7 * lauf);
            edit->setEnabled(false);
            edit->setAlignment(Qt::AlignCenter);
            edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            myQVBoxLayout->addWidget(edit);

            mainQHBoxLayout->addWidget(itemQWidget);
        }
        mainQHBoxLayout->setSpacing(2);
        mainQHBoxLayout->addStretch();

        trayQWidget->show();
    }

    { //Add layouts as child layouts
        QHBoxLayout* mainQHBoxLayout = new QHBoxLayout();
        QWidget* trayQWidget = new QWidget();
        trayQWidget->setLayout(mainQHBoxLayout);

        for (int lauf = 10; lauf < 15; lauf++)
        {
            QVBoxLayout* myQVBoxLayout = new QVBoxLayout();
            myQVBoxLayout->addStretch();

            QTextEdit* edit = new QTextEdit;
            edit->setText(QString("Child layouts %1").arg(lauf));
            edit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
            edit->setMaximumWidth(8 * lauf);
            edit->setMaximumHeight(7 * lauf);
            edit->setEnabled(false);
            edit->setAlignment(Qt::AlignCenter);
            edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            myQVBoxLayout->addWidget(edit);
            mainQHBoxLayout->addLayout(myQVBoxLayout);
        }
        mainQHBoxLayout->setSpacing(2);
        mainQHBoxLayout->addStretch();

        trayQWidget->show();
    }


    return app.exec();
}