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!!
Absolute
widget which contains 2QTextEdit
:ledit
,lledit
. You want to show scrollbar in theAbsolute
widget when onlylledit
overflowsAbsolute
's width? – pat