2

Im new to Qt programming and I want to add a scrollbar to a widget which is having child widgets within it.I have seen several questions/posts about this like:

1.How to add a scrollbar to parent QWidget

2.Insert a scrollbar in a qt widget using qtcreator

3.Adding scroll bar to a Qwidget

4.QScrollArea missing Scrollbar

But most of the answers set a layout to the widget for which we add the scrollbar.

My Problem:

The widget for which I need scrollbar has many child widgets within it.But I haven't added any layout to it.The geometry of the child widgets are modifiable and so I haven't added any layout to the parent widget.

Below is my code:

class Absolute : public QWidget {

public:
 Absolute(QWidget *parent = 0);
};

Absolute::Absolute(QWidget *parent)
 : QWidget(parent) {

QTextEdit *ledit = new QTextEdit(this);
ledit->setGeometry(5, 5, 500, 550);

QTextEdit *lledit = new QTextEdit(this);
lledit->setGeometry(510, 5, 250, 550);

/*QScrollArea* sa = new QScrollArea();
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
auto *widget = new QWidget(this);
sa->setWidget(widget);

auto *l = new QVBoxLayout(this);
l->setMargin(0);
l->addWidget(sa);*/
}

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

QApplication app(argc, argv);

Absolute window;
window.setWindowTitle("Absolute");
window.setGeometry(500,500,1500,1000);
window.show();

return app.exec();
}

However without the scrollbar code(commented portion),the UI has those textedits in the given position as set in the setGeometry.

All I need is to bring a scrollbar if the 2nd textedits width is more.

So I tried adding the scrollbar(the commented portion).However I can see only the scrollbar and not the textedits.

Any suggestion/inputs will be really helpful.Thanks in advance!!

scopchanov
  • 7,966
  • 10
  • 40
  • 68
george
  • 339
  • 3
  • 12
  • You have an `Absolute` widget which contains 2 `QTextEdit`: `ledit`, `lledit`. You want to show scrollbar in the `Absolute` widget when only `lledit` overflows`Absolute`'s width? – pat Sep 19 '18 at 10:34

1 Answers1

1

Cause

The way you set the parents when you create the widgets and layouts is not correct.

Solution

Create the correct parent/child hierarchy and set the desired size of the QScrollArea's widget. There is no need to set a layout to this widget.

Example

Here is an example I have prepared for you in order to demonstrate how you could fix Absolute:

class Absolute : public QWidget {
public:
    Absolute::Absolute(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        auto *sa = new QScrollArea(this);
        auto *l = new QVBoxLayout(this);
        auto *widget = new QWidget();
        auto *ledit = new QTextEdit(widget);
        auto *lledit = new QTextEdit(widget);

        sa->setWidgetResizable(true);
        sa->setWidget(widget);
        sa->setAlignment(Qt::AlignLeft | Qt::AlignTop);

        ledit->setGeometry(5, 5, 500, 550);
        lledit->setGeometry(510, 5, 250, 550);

        widget->setFixedSize(lledit->geometry().right(), lledit->geometry().bottom());

        l->setMargin(0);
        l->addWidget(sa);
    }
};

Note: For demonstration purposes the size of widget is set to (lledit->geometry().right(), lledit->geometry().bottom()). You might consider adjusting it according to your specific needs.

Community
  • 1
  • 1
scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • 1
    Apologies for the delayed reply..Thanks @scopchanov..it is working!!Could you brief me the significance of creating a new widget and QVBoxLayout(this)? – george Sep 19 '18 at 17:46
  • @george, about _the significance of creating a new widget_: `QScrollArea` needs a widget to operete on. `Absolute` is a widget, which contains `QScrollArea`, which contains widget. Sounds complicated, but it is not - just a proper widget nesting (parenting). The same is with the layout. `Absolute` needs a layout in order to resize the `QScrollArea` when it is resized itself. – scopchanov Sep 19 '18 at 17:50
  • @george, in general the following is done: you add a widget. Then you set a layout to it (depending on how you want to arrange the subwidgets it contains) and then you add those subwidgets to this layout. If a subwidget has a sub-subwidget you repeat the process, but one level lower. – scopchanov Sep 19 '18 at 17:54
  • @george, in this case consider `Absolute` as the widget, `sa` as the sub-widget and `widget` as the sub-sub-widget. – scopchanov Sep 19 '18 at 17:57
  • @Thanks for the explanation!! Does it mean, ledit and lledit are not the direct children of Absolute? and are direct children of the newly created widget? is the new widget an imaginary sort of one? – george Sep 19 '18 at 18:19
  • @george, yes: `ledit` and `lledit` are not direct children of `Absolute`. They are children of `widget`, which is a child of `Absolute`. `widget` is not imaginery, it is just not rendered on the screen. But if you set its background to an appropriate color, you will see it. – scopchanov Sep 19 '18 at 18:28
  • ::Thanks for the reply again..the code works without passing this to **new ScrollArea**..is it not mandatory?Could u suggest me some tutorials/links from where I can understand this layouting/parent-child hierarchy!! – george Sep 19 '18 at 18:46
  • @george, it could be that the parent is set when you add it to the layout. I don't remember exactly. Anyway, take a look at [these episodes](https://www.youtube.com/watch?v=6KtOzh0StTc&list=PL7C604D492F9E0E0B). They are already a bit old, but definitely a good start into Qt and GUI programming. – scopchanov Sep 19 '18 at 18:51
  • @george and most importantly - don't stop having fun with it. If you have other questions in this regard, you are very welcome to ask them here. – scopchanov Sep 19 '18 at 18:53