Modernize rasterwindow/openglwindow examples to use requestUpdate()

Change-Id: Ib8d0c42db7343247d0431ea008eb17da9ee98f4d
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
This commit is contained in:
Tor Arne Vestbø 2016-09-14 15:01:13 +02:00 committed by Tor Arne Vestbø
parent 524b59b0f5
commit 0deb0796a4
7 changed files with 23 additions and 34 deletions

View File

@ -34,6 +34,9 @@
\image openglwindow-example.png Screenshot of the OpenGLWindow example \image openglwindow-example.png Screenshot of the OpenGLWindow example
\note This is a low level example of how to use QWindow with OpenGL.
In practice you should consider using the higher level QOpenGLWindow class.
\section1 OpenGLWindow Super Class \section1 OpenGLWindow Super Class
Our OpenGLWindow class acts as an API which is then subclassed to do the Our OpenGLWindow class acts as an API which is then subclassed to do the
@ -70,9 +73,8 @@
\snippet openglwindow/openglwindow.cpp 2 \snippet openglwindow/openglwindow.cpp 2
The renderLater() function simply puts an update request event on The renderLater() function simply calls QWindow::requestUpdate() to schedule
the event loop, which leads to renderNow() being called once the event an update for when the system is ready to repaint.
gets processed.
We also call renderNow() when we get an expose event. The exposeEvent() is We also call renderNow() when we get an expose event. The exposeEvent() is
the notification to the window that its exposure, meaning visibility, on the notification to the window that its exposure, meaning visibility, on
@ -109,11 +111,11 @@
\l {http://www.khronos.org/registry/gles/}{Khronos OpenGL ES API Registry}. \l {http://www.khronos.org/registry/gles/}{Khronos OpenGL ES API Registry}.
If animation has been enabled with OpenGLWindow::setAnimating(true), we If animation has been enabled with OpenGLWindow::setAnimating(true), we
call renderLater() to put another update request on the event loop. call renderLater() to schedule another update request.
\snippet openglwindow/openglwindow.cpp 4 \snippet openglwindow/openglwindow.cpp 4
Enabling animation also triggers an update request as shown in the Enabling animation also schedules an update request as shown in the
following code snippet. following code snippet.
\snippet openglwindow/openglwindow.cpp 5 \snippet openglwindow/openglwindow.cpp 5

View File

@ -143,19 +143,16 @@
\snippet rasterwindow/rasterwindow.cpp 6 \snippet rasterwindow/rasterwindow.cpp 6
We went through a few places where the window needed to repainted We went through a few places where the window needed to repainted
immediately. There are some cases where this is not desierable, immediately. There are some cases where this is not desirable,
but rather let the application return to the event loop and but rather let the application return to the event loop and
later. We acheive this by posting an even to ourself which will schedule the repaint for later. We achieve this by requesting
then be delivered when the application returns to the \l an update, using QWindow::requestUpdate(), which will then be
QGuiApplication event loop. To avoid posting new requests when one delivered when the system is ready to repaint.
is already pending, we store this state in the \c m_update_pending
variable.
\snippet rasterwindow/rasterwindow.cpp 7 \snippet rasterwindow/rasterwindow.cpp 7
We reimplement the virtual \l QObject::event() function to handle We reimplement the virtual \l QObject::event() function to handle
the update event we posted to ourselves. When the event comes in the update event. When the event comes in we call renderNow() to
we reset the pending update flag and call renderNow() to render render the window right away.
the window right away.
*/ */

View File

@ -59,7 +59,6 @@
//! [1] //! [1]
OpenGLWindow::OpenGLWindow(QWindow *parent) OpenGLWindow::OpenGLWindow(QWindow *parent)
: QWindow(parent) : QWindow(parent)
, m_update_pending(false)
, m_animating(false) , m_animating(false)
, m_context(0) , m_context(0)
, m_device(0) , m_device(0)
@ -99,17 +98,13 @@ void OpenGLWindow::render()
//! [3] //! [3]
void OpenGLWindow::renderLater() void OpenGLWindow::renderLater()
{ {
if (!m_update_pending) { requestUpdate();
m_update_pending = true;
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
} }
bool OpenGLWindow::event(QEvent *event) bool OpenGLWindow::event(QEvent *event)
{ {
switch (event->type()) { switch (event->type()) {
case QEvent::UpdateRequest: case QEvent::UpdateRequest:
m_update_pending = false;
renderNow(); renderNow();
return true; return true;
default: default:

View File

@ -82,7 +82,6 @@ protected:
void exposeEvent(QExposeEvent *event) override; void exposeEvent(QExposeEvent *event) override;
private: private:
bool m_update_pending;
bool m_animating; bool m_animating;
QOpenGLContext *m_context; QOpenGLContext *m_context;

View File

@ -53,7 +53,6 @@
//! [1] //! [1]
RasterWindow::RasterWindow(QWindow *parent) RasterWindow::RasterWindow(QWindow *parent)
: QWindow(parent) : QWindow(parent)
, m_update_pending(false)
{ {
create(); create();
m_backingStore = new QBackingStore(this); m_backingStore = new QBackingStore(this);
@ -68,7 +67,6 @@ RasterWindow::RasterWindow(QWindow *parent)
bool RasterWindow::event(QEvent *event) bool RasterWindow::event(QEvent *event)
{ {
if (event->type() == QEvent::UpdateRequest) { if (event->type() == QEvent::UpdateRequest) {
m_update_pending = false;
renderNow(); renderNow();
return true; return true;
} }
@ -79,10 +77,7 @@ bool RasterWindow::event(QEvent *event)
//! [6] //! [6]
void RasterWindow::renderLater() void RasterWindow::renderLater()
{ {
if (!m_update_pending) { requestUpdate();
m_update_pending = true;
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
} }
//! [6] //! [6]
@ -99,9 +94,8 @@ void RasterWindow::resizeEvent(QResizeEvent *resizeEvent)
//! [2] //! [2]
void RasterWindow::exposeEvent(QExposeEvent *) void RasterWindow::exposeEvent(QExposeEvent *)
{ {
if (isExposed()) { if (isExposed())
renderNow(); renderNow();
}
} }
//! [2] //! [2]

View File

@ -74,7 +74,6 @@ protected:
private: private:
QBackingStore *m_backingStore; QBackingStore *m_backingStore;
bool m_update_pending;
}; };
//! [1] //! [1]
#endif // RASTERWINDOW_H #endif // RASTERWINDOW_H

View File

@ -2175,11 +2175,14 @@ void QWindowPrivate::deliverUpdateRequest()
Schedules a QEvent::UpdateRequest event to be delivered to this window. Schedules a QEvent::UpdateRequest event to be delivered to this window.
The event is delivered in sync with the display vsync on platforms The event is delivered in sync with the display vsync on platforms
where this is possible. When driving animations, this function should where this is possible. Otherwise, the event is delivered after a
be called once after drawing has completed. delay of 5 ms. The additional time is there to give the event loop
a bit of idle time to gather system events, and can be overridden
using the QT_QPA_UPDATE_IDLE_TIME environment variable.
Calling this function multiple times will result in a single event When driving animations, this function should be called once after drawing
being delivered to the window. has completed. Calling this function multiple times will result in a single
event being delivered to the window.
Subclasses of QWindow should reimplement event(), intercept the event and Subclasses of QWindow should reimplement event(), intercept the event and
call the application's rendering code, then call the base class call the application's rendering code, then call the base class