summaryrefslogtreecommitdiffstats
path: root/src/gui/doc/examples/rasterwindow.qdoc
blob: d78f003b40355f24ae2ef129149244e749158a1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms
** and conditions contained in a signed written agreement between you
** and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example gui/rasterwindow
    \title Raster Window Example

    This example shows how to create a minimal QWindow based
    application using QPainter for rendering.


    \section1 Application Entry Point

    \snippet gui/rasterwindow/main.cpp 1

    The entry point for a QWindow based application is the \l
    QGuiApplication class. It manages the GUI application's control
    flow and main settings. We pass the command line arguments which
    can be used to pick up certain system wide options.

    From there, we go on to create our window instance and then call
    the \l QWindow::show() function to tell the windowing system that
    this window should now be made visible on screen.

    Once this is done, we enter the application's event loop so the
    application can run.


    \section1 RasterWindow Declaration

    \snippet gui/rasterwindow/rasterwindow.h 1

    We first start by including the the QtGui headers. This means we
    can use all classes in the Qt GUI module. Classes can also be
    included individually if that is preferred.

    The RasterWindow class subclasses QWindow directly and provides a
    constructor which allows the window to be a sub-window of another
    QWindow. Parent-less QWindows show up in the windowing system as
    top-level windows.

    The class declares a QBackingStore which is what we use to manage
    the window's back buffer for QPainter based graphics.

    \e {The raster window is also reused in a few other examples and adds
    a few helper functions, like renderLater().}


    \section1 RasterWindow Implementation

    \snippet gui/rasterwindow/rasterwindow.cpp 1

    The constructor first of all calls \l QWindow::create(). This will
    create the window in the windowing system. Without calling create,
    the window will not get events and will not be visible in the
    windowing system. The call to create does not show the window. We
    then set the geometry to be something reasonable.

    Then we create the backingstore and pass it the window instance it
    is supposed to manage.

    \snippet gui/rasterwindow/rasterwindow.cpp 2

    Shortly after calling \l QWindow::show() on a created window, the
    virtual function \l QWindow::exposeEvent() will be called to
    notify us that the window's exposure in the windowing system has
    changed. The event contains the exposed sub-region, but since we
    will anyway draw the entire window every time, we do not make use
    of that.

    The function \l QWindow::isExposed() will tell us if the window is
    showing or not. We need this as the exposeEvent is called also
    when the window becomes obscured in the windowing system. If the
    window is showing, we call renderNow() to draw the window
    immediately. We want to draw right away so we can present the
    system with some visual content.


    \snippet gui/rasterwindow/rasterwindow.cpp 5

    The resize event is guaranteed to be called prior to the window
    being shown on screen and will also be called whenever the window
    is resized while on screen. We use this to resize the back buffer
    and call renderNow() if we are visible to immediately update the
    visual representation of the window on screen.

    \snippet gui/rasterwindow/rasterwindow.cpp 3

    The renderNow function sets up what is needed for a \l QWindow to
    render its content using QPainter. As obscured windows have will
    not be visible, we abort if the window is not exposed in the
    windowing system. This can for instance happen when another window
    fully obscures this window.

    We start the drawing by calling \l QBackingStore::beginPaint() on
    the region we want to draw. Then we get the \l QPaintDevice of the
    back buffer and create a QPainter to render to that paint device.

    To void leaving traces from the previous rendering and start with a
    clean buffer, we fill the entire buffer with the color white. Then
    we call the virtual render() function which does the actual
    drawing of this window.

    After drawing is complete, we call endPaint() to signal that we
    are done rendering and present the contents in the back buffer
    using \l QBackingStore::flush().


    \snippet gui/rasterwindow/rasterwindow.cpp 4

    The render function contains the drawing code for the window. In
    this minial example, we only draw the string "QWindow" in the
    center.


    \section1 Rendering Asynchronously


    \snippet gui/rasterwindow/rasterwindow.cpp 6

    We went through a few places where the window needed to repainted
    immediately. There are some cases where this is not desierable,
    but rather let the application return to the event loop and
    later. We acheive this by posting an even to ourself which will
    then be delivered when the application returns to the \l
    QGuiApplication event loop. To avoid posting new requests when one
    is already pending, we store this state in the \c m_update_pending
    variable.

    \snippet gui/rasterwindow/rasterwindow.cpp 7

    We reimplement the virtual \l QObject::event() function to handle
    the update event we posted to ourselves. When the event comes in
    we reset the pending update flag and call renderNow() to render
    the window right away.

  */