Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
myscrollarea.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtWidgets>
5
7{
8public:
9 MyScrollArea(QWidget *w);
10 void setWidget(QWidget *w);
11
12protected:
13 void scrollContentsBy(int dx, int dy) override;
14 void resizeEvent(QResizeEvent *event) override;
15
16private:
17 void updateWidgetPosition();
18 void updateArea();
19
20 QWidget *widget;
21};
22
23MyScrollArea::MyScrollArea(QWidget *widget)
25{
26 setWidget(widget);
27}
28
29void MyScrollArea::setWidget(QWidget *w)
30{
31 widget = w;
32 widget->setParent(viewport());
33 if (!widget->testAttribute(Qt::WA_Resized))
34 widget->resize(widget->sizeHint());
35
36 verticalScrollBar()->setValue(0);
37 verticalScrollBar()->setValue(0);
38
39 updateArea();
40}
41
42void MyScrollArea::updateWidgetPosition()
43{
44 //! [0]
45 int hvalue = horizontalScrollBar()->value();
46 int vvalue = verticalScrollBar()->value();
47 QPoint topLeft = viewport()->rect().topLeft();
48
49 widget->move(topLeft.x() - hvalue, topLeft.y() - vvalue);
50 //! [0]
51}
52
53void MyScrollArea::scrollContentsBy(int dx, int dy)
54{
55 Q_UNUSED(dx);
56 Q_UNUSED(dy);
57 updateWidgetPosition();
58}
59
60void MyScrollArea::updateArea()
61{
62 //! [1]
63 QSize areaSize = viewport()->size();
64 QSize widgetSize = widget->size();
65
66 verticalScrollBar()->setPageStep(areaSize.height());
67 horizontalScrollBar()->setPageStep(areaSize.width());
68 verticalScrollBar()->setRange(0, widgetSize.height() - areaSize.height());
69 horizontalScrollBar()->setRange(0, widgetSize.width() - areaSize.width());
70 updateWidgetPosition();
71 //! [1]
72}
73
74void MyScrollArea::resizeEvent(QResizeEvent *event)
75{
76 Q_UNUSED(event);
77 updateArea();
78}
void resizeEvent(QResizeEvent *event) override
MyScrollArea(QWidget *w)
void setWidget(QWidget *w)
void scrollContentsBy(int dx, int dy) override