Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
6class MyScrollArea : public QAbstractScrollArea
7{
8public:
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
24 : QAbstractScrollArea()
25{
27}
28
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{
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);
51}
52
54{
55 Q_UNUSED(dx);
56 Q_UNUSED(dy);
57 updateWidgetPosition();
58}
59
60void MyScrollArea::updateArea()
61{
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();
72}
73
75{
77 updateArea();
78}
void resizeEvent(QResizeEvent *event) override
MyScrollArea(QWidget *w)
void setWidget(QWidget *w)
void scrollContentsBy(int dx, int dy) override
\inmodule QtCore\reentrant
Definition qpoint.h:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:548
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
QSize size
the size of the widget excluding any window frame
Definition qwidget.h:113
void move(int x, int y)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:880
QSize sizeHint
the recommended size for the widget
Definition qwidget.h:148
void resize(int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:883
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition qwidget.h:910
QOpenGLWidget * widget
[1]
@ WA_Resized
Definition qnamespace.h:308
GLfloat GLfloat GLfloat w
[0]
struct _cl_event * event
#define Q_UNUSED(x)
view viewport() -> scroll(dx, dy, deviceRect)