Search code examples
c++qtqlineeditqlayoutqt5.9

Why does the setspacing attribute not work?


Im new to Qt and experimenting it.I have a layout whose code is given below:

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *parentLayout = new QVBoxLayout(this);//MainWindow is a QWidget

this->setStyleSheet("background-color:red");

for(int i=0;i<3;i++){
QHBoxLayout* labelLineEdit = f1();

parentLayout->addLayout(labelLineEdit);
}
parentLayout->setContentsMargins(0,0,40,0);
}

QHBoxLayout* MainWindow::f1()
{

QHBoxLayout *layout = new QHBoxLayout;

QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);

layout->addWidget(label);

QLineEdit *echoLineEdit = new QLineEdit;
//echoLineEdit->setMaximumWidth(120);//line:99
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);

echoLineEdit->setStyleSheet("background-color:brown");

layout->addWidget(echoLineEdit);

layout->setSpacing(0);

return layout;

}

And my output looks like this.enter image description here

I want my lineedit width to be reduced,so I uncommented the line 99 and my output looks like the below.enter image description here

The setspacing and setContentsMargins attributes doesn't work in this case. Where am I going wrong.Anyhelp will be really useful.


Solution

  • If you have an automatic layout, something should take up the empty space. If the policy of the widget(s) is set to QSizePolicy::Expanding it is the widget(s) who will be expanded to fill the blank. If you make the size of the widget(s) fixed (QSizePolicy::Fixed) or limit its/their size with setMaximum... the empty space will be distributed throughout the layout. If this is not desirable, as in your case, something should be added to the layout to take this empty space up. You have a couple of options. I personally would go by using QBoxLayout::addStretch instead of QSpacerItem. Here is the solution, as well as a little bit of cleanup of the code from the question:

    #include "MainWindow.h"
    #include <QHBoxLayout>
    #include <QLineEdit>
    #include <QLabel>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        auto *widget = new QWidget(this);
        auto *layoutMain = new QVBoxLayout(widget);
    
        for (int n = 0; n < 3; n++)
            f1(layoutMain);
    
        layoutMain->setContentsMargins(0, 0, 40, 0);
        layoutMain->addStretch();
    
        setCentralWidget(widget);
        setStyleSheet("background-color: red");
    }
    
    void MainWindow::f1(QVBoxLayout *layoutMain)
    {
        auto *layoutRow = new QHBoxLayout();
        auto *label = new QLabel("Movie", this);
        auto *lineEdit = new QLineEdit(this);
    
        label->setStyleSheet("background-color: blue; color: white");
        label->setFixedWidth(300);
    
        lineEdit->setMaximumWidth(120);
        lineEdit->setFixedHeight(50);
        lineEdit->setStyleSheet("background-color: brown");
    
        layoutRow->addWidget(label);
        layoutRow->addWidget(lineEdit);
        layoutRow->addStretch();
        layoutRow->setSpacing(0);
    
        layoutMain->addLayout(layoutRow);
    }
    

    This produces the following result:

    enter image description here

    If you want the empty space to be at the beginning of each row, effectively aligning the widgets to the right, just put the line layoutRow->addStretch(); before layoutRow->addWidget(label);. To center the widgets horizontally, add another stretch, so that there is one before and one after them. The same way you could center the widgets vertically adding layoutMain->addStretch(); before for (int n = 0; n < 3; n++).