2
votes

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!!

1
You have an Absolute widget which contains 2 QTextEdit: ledit, lledit. You want to show scrollbar in the Absolute widget when only lledit overflowsAbsolute's width?pat

1 Answers

1
votes

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.