QScrollArea size is the same with is child widget but it display scroller bar.
-
Hi, I want to use QScrollArea to adjust my widget size.But I found when I set QScrollArea size with my widget's size, the scroller bar is shown instead of hidden. Why? I set the QScrollArea with my widget size in the function "void AppTabScrollArea::resizeEvent(QResizeEvent* event)".
Below it is my code.
The header:#pragma once #include <QScrollArea> #include <QMdiArea> class AppTabScrollArea :public QScrollArea { Q_OBJECT public: AppTabScrollArea(QWidget* parent = nullptr); QWidget* testWidget = nullptr; protected: void resizeEvent(QResizeEvent* event) override; };
the source:
#include "QtWidgetsApplication1.h" #include <QtWidgets/QApplication> #include "TestScrollArea.h" AppTabScrollArea::AppTabScrollArea(QWidget* parent):QScrollArea(parent) { } void AppTabScrollArea::resizeEvent(QResizeEvent* event) { QScrollArea::resizeEvent(event); if (testWidget != nullptr) { QSize sizeTemp = size(); testWidget->resize(width(), height()); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); QtWidgetsApplication1 w; w.show(); return a.exec(); }
the test:
#include "QtWidgetsApplication1.h" #include "TestScrollArea.h" #include <qpushbutton.h> QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent) : QMainWindow(parent) { AppTabScrollArea* scrollArea = new AppTabScrollArea(); scrollArea->setBackgroundRole(QPalette::Dark); QPushButton* btn = new QPushButton(); btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); btn->setText("Test"); scrollArea->setWidget(btn); scrollArea->setWidgetResizable(false); scrollArea->testWidget = btn; setCentralWidget(scrollArea); } QtWidgetsApplication1::~QtWidgetsApplication1() {}
-
@jinming said in QScrollArea size is the same with is child widget but it display scroller bar.:
I want to use QScrollArea to adjust my widget size
Unless there's another reason for it, better use layouts instead of a
QScrollArea
.
Especially when you don't plan to resize the content widget of theQScrollArea
anyway.scrollArea->setWidgetResizable(false);
If you find a reason to still use a scroll area, you can turn the scroll bars off with
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-
@Pl45m4 Hi, I want to use QScrollArea to display a widget with variable content size. But I've seen people on the Internet say that the scroll bar will only be displayed when the size of the widget is larger than the size of the QScrollArea, and less than or equal to not displaying the scroll bar. Now I set
scrollArea->setWidgetResizable(false); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
How can I know the size of the widget when the slider is hidden.
Thanks a lot. -
@jinming said in QScrollArea size is the same with is child widget but it display scroller bar.:
How can I know the size of the widget
By calling QWidget::size()? Or what do you mean?
"I want to use QScrollArea to adjust my widget size" - I also don't understand this. QScrollArea is not used to adjust widget size...
-
@jsulm Hi, QScrollArea is used to adjust widget's view by scroll bar, I will adjust the QScrollArea child widget size, if the child widget is larger than the size of QScrollArea, we can use scroll bar to adjust the view of the widget.Now how can I set the widget size when the scroll bar is hidden? The QScrollArea scroll bar policy is Qt::ScrollBarAsNeeded not Qt::ScrollBarAlwaysOff.
-
@jinming said in QScrollArea size is the same with is child widget but it display scroller bar.:
Now how can I set the widget size when the scroll bar is hidden? The QScrollArea scroll bar policy is Qt::ScrollBarAsNeeded not Qt::ScrollBarAlwaysOff.
You don't set any size... The scroll area is just to show the relevant parts of a widget inside the area, which is larger than the view port.
When set toQt::ScrollBarAsNeeded
the scroll bars appear as soon as the widget is close to the size of the view port (don't know the exact threshold)@jinming said in QScrollArea size is the same with is child widget but it display scroller bar.:
void AppTabScrollArea::resizeEvent(QResizeEvent* event) { QScrollArea::resizeEvent(event); if (testWidget != nullptr) { QSize sizeTemp = size(); testWidget->resize(width(), height()); } }
and because of this, you always see the scroll bars, because the
QScrollArea
decided that they are needed.Try
testWidget->resize(width() - 5, height() - 5);
and you should not see your scroll bars permanently.
Another option is to turn them off completely, as I've said before.
Also it makes not such sense to continuously resize the content widget inside the scroll area to fit the view port...
Then you don't need aQScrollArea
at all.
Usually you set a widget with huge dimension e.g. which is holding an image (like 1920x1080) to a scroll area, where the view port and the window is only a fraction of it... then you can scroll the image without resizing the inner widget.See here:
-