1

To my custom widget, inherited from QWidget, I have added a QScrollArea like this:

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)//MainWindow is a QWidget
{
    auto *scrollArea = new QScrollArea(this);
    auto *widget = new QWidget(this);

    widget->setStyleSheet("background-color:green");

    scrollArea->setWidget(widget);
    scrollArea->setWidgetResizable(true);
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    QVBoxLayout *parentLayout = new QVBoxLayout(widget);

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

    for(int i=0;i<12;i++){
        QHBoxLayout* labelLineEdit = f1();
        parentLayout->addStretch(1);
        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);
    echoLineEdit->setMaximumHeight(50);
    echoLineEdit->setMinimumHeight(50);

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

    layout->addWidget(echoLineEdit);

    layout->setSpacing(0);

    return layout;
}

This produces a window which looks like this:

enter image description here

The problem is, that I want the scrollArea to occupy the entire window, but it does not. It also doesn't get resized when I resize the window.

How could I fix this?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
adi
  • 984
  • 15
  • 33
  • Why your _MainWindow_ is inheriting from _QWidget_ and not from _QMainWindow_? – scopchanov Aug 14 '18 at 15:09
  • add the following at the end of the constructor of _MainWindow_ (after `parentLayout->setContentsMargins(0,0,40,0);`): `auto *l = new QVBoxLayout(this); l->addWidget(scrollArea);` – scopchanov Aug 14 '18 at 15:14
  • @scopchanov Your question does not make sense, that is, the MainWindow class must necessarily inherit from QMainWindow, where is it indicated? – eyllanesc Aug 14 '18 at 15:14
  • @eyllanesc, I am asking if there is a special reason not to inherit from _QMainWindow_. – scopchanov Aug 14 '18 at 15:16
  • @eyllanesc, because if he inherits from _QMainWindow_, the solution would be to just add: `setCentralWidget(scrollArea);`. – scopchanov Aug 14 '18 at 15:17
  • @scopchanov mmm, but it would be to modify what the asker wants, do not you think?, the solution is just to set a layout to the widget, and in that layout to set the QScrollArea, that is similar to setCentralWidget. – eyllanesc Aug 14 '18 at 15:20
  • @eyllanesc, you're right. That's why my next comment was proposing this solution with the layout. – scopchanov Aug 14 '18 at 15:22
  • @scopchanov I also think that this question has been asked many times, later I will search for a duplicate. :) – eyllanesc Aug 14 '18 at 15:23
  • @eyllanesc, that's why I am not in a hurry to start composing an answer ;) – scopchanov Aug 14 '18 at 15:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178021/discussion-between-scopchanov-and-eyllanesc). – scopchanov Aug 14 '18 at 15:34
  • The above code is a part of a custom widget,so I inherited QWidget and not QMainWindow. – adi Aug 14 '18 at 15:38
  • @adi, and does the solution work for you? – scopchanov Aug 14 '18 at 17:16
  • @scopchanov..Its working man..apologies for the delayed reply..could you add this as answer.. – adi Aug 14 '18 at 19:33
  • @scopchanov..could you add this as answer so that I can mark it and could u pls brief the significance of those lines so that I can understand the solution correctly.... – adi Aug 14 '18 at 19:39
  • 1
    @scopchanov:If you dont mind,could you have a look at this https://stackoverflow.com/questions/52426751/qtunable-to-set-custom-widget-background-color-border-width] question!! – adi Sep 20 '18 at 14:03

1 Answers1

1

The problem is, that I want the scrollArea to occupy the entire window, but it does not. It also doesn't get resized when I resize the window.

The reason is that you have not set any kind of layout to manage the positioning of your QScrollArea widget itself, so it is just being left to its own devices (and therefore it just chooses a default size-and-location for itself and stays at that size-and-location).

A simple fix would be to add these lines to the bottom of your MainWindow constructor:

QBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(0);
mainLayout->addWidget(scrollArea);
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234