1

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.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
adi
  • 984
  • 15
  • 33

2 Answers2

3

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++).

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • Thank u @scopchanov..the expanation is gr8.one question:you told "the empty space will be distributed throughout the layout - when we use setMaxim...calls",is this the reason the setSpacing and setContentsMargin calls are not working? – adi Aug 14 '18 at 11:46
  • @adi Exactly. It is working, but you see more space than you have set, because it is the spacing _+_ the distributed empty space. – scopchanov Aug 14 '18 at 11:48
  • @adi, try my code with `layoutRow->setSpacing(6);` and comment out all `layoutRow->addStretch();` lines and you will see, that the space between the labels and the line edits will never be less than 6. – scopchanov Aug 14 '18 at 11:50
  • "the distributed empty space"-could you share me some qt links for this:it seems interesting!! – adi Aug 14 '18 at 11:51
  • @adi, I am sorry! I am not able to find this written explicitly. – scopchanov Aug 14 '18 at 13:20
  • is it ! thats fyn @scopchanov..could u give me an idea of how to add a scrollbar for layoutmain? – adi Aug 14 '18 at 13:26
  • @adi consider this updated code: `#include "MainWindow.h" #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { auto *scrollArea = new QScrollArea(this); auto *widget = new QWidget(this); auto *layoutMain = new QVBoxLayout(widget); scrollArea->setWidget(widget); scrollArea->setWidgetResizable(true); setStyleSheet("background-color: red"); layoutMain->addStretch(); for (int n = 0; n < 3; n++) f1(layoutMain); layoutMain->setContentsMargins(0, 0, 0, 0); layoutMain->addStretch(); setCentralWidget(scrollArea); }` – scopchanov Aug 14 '18 at 13:33
  • Thanks for the updated code @scopchanov..One small issue,Im able to add scrollbar;I tried adding more widgets and all of them are inside the scrollbar but the scrollbar is getting restricted to half the size of the widget.Could you suggest me some fix!! – adi Aug 14 '18 at 14:17
  • @adi, try to add the widgets not to the scroll bar directly, but to the _layoutMain_. – scopchanov Aug 14 '18 at 14:27
  • shall I ask this as a separate question ? – adi Aug 14 '18 at 14:29
  • @adi, I think that would be more apropriate and I would be glad to try to help you if I could. – scopchanov Aug 14 '18 at 14:30
  • pls refer to https://stackoverflow.com/questions/51844426/qt-scrollbar-getting-restricted-in-size – adi Aug 14 '18 at 14:59
  • Could you tell me what u meant by " automatic layout"? – adi Aug 16 '18 at 05:41
  • @adi, I will elaborate on this later today as an answer to your other question. – scopchanov Aug 16 '18 at 05:56
  • @adi, ask a separate question about this, i.e. how automatic layout works in Qt. – scopchanov Aug 29 '18 at 16:05
2

You should add a spacer item for each row (refer QSpacerItem)

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);

    //add spacer here
    QSpacerItem * item = new QSpacerItem(100, 1, QSizePolicy::Expanding, QSizePolicy::Fixed);
    layout->addItem(item);

    layout->setSpacing(0);

    return layout;
}
pat
  • 497
  • 3
  • 12
  • Hey thanks man,its working..could u pls tell me how adding a spacer solved this issue?And what is the significance of specifying 100 and 1 in additon to the size policy? – adi Aug 14 '18 at 10:58