Modernize rasterwindow/openglwindow examples to use requestUpdate()
Change-Id: Ib8d0c42db7343247d0431ea008eb17da9ee98f4d Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
This commit is contained in:
parent
524b59b0f5
commit
0deb0796a4
@ -34,6 +34,9 @@
|
||||
|
||||
\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
|
||||
|
||||
Our OpenGLWindow class acts as an API which is then subclassed to do the
|
||||
@ -70,9 +73,8 @@
|
||||
|
||||
\snippet openglwindow/openglwindow.cpp 2
|
||||
|
||||
The renderLater() function simply puts an update request event on
|
||||
the event loop, which leads to renderNow() being called once the event
|
||||
gets processed.
|
||||
The renderLater() function simply calls QWindow::requestUpdate() to schedule
|
||||
an update for when the system is ready to repaint.
|
||||
|
||||
We also call renderNow() when we get an expose event. The exposeEvent() is
|
||||
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}.
|
||||
|
||||
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
|
||||
|
||||
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.
|
||||
|
||||
\snippet openglwindow/openglwindow.cpp 5
|
||||
|
@ -143,19 +143,16 @@
|
||||
\snippet 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,
|
||||
immediately. There are some cases where this is not desirable,
|
||||
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.
|
||||
schedule the repaint for later. We achieve this by requesting
|
||||
an update, using QWindow::requestUpdate(), which will then be
|
||||
delivered when the system is ready to repaint.
|
||||
|
||||
\snippet 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.
|
||||
the update event. When the event comes in we call renderNow() to
|
||||
render the window right away.
|
||||
|
||||
*/
|
||||
|
@ -59,7 +59,6 @@
|
||||
//! [1]
|
||||
OpenGLWindow::OpenGLWindow(QWindow *parent)
|
||||
: QWindow(parent)
|
||||
, m_update_pending(false)
|
||||
, m_animating(false)
|
||||
, m_context(0)
|
||||
, m_device(0)
|
||||
@ -99,17 +98,13 @@ void OpenGLWindow::render()
|
||||
//! [3]
|
||||
void OpenGLWindow::renderLater()
|
||||
{
|
||||
if (!m_update_pending) {
|
||||
m_update_pending = true;
|
||||
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
|
||||
}
|
||||
requestUpdate();
|
||||
}
|
||||
|
||||
bool OpenGLWindow::event(QEvent *event)
|
||||
{
|
||||
switch (event->type()) {
|
||||
case QEvent::UpdateRequest:
|
||||
m_update_pending = false;
|
||||
renderNow();
|
||||
return true;
|
||||
default:
|
||||
|
@ -82,7 +82,6 @@ protected:
|
||||
void exposeEvent(QExposeEvent *event) override;
|
||||
|
||||
private:
|
||||
bool m_update_pending;
|
||||
bool m_animating;
|
||||
|
||||
QOpenGLContext *m_context;
|
||||
|
@ -53,7 +53,6 @@
|
||||
//! [1]
|
||||
RasterWindow::RasterWindow(QWindow *parent)
|
||||
: QWindow(parent)
|
||||
, m_update_pending(false)
|
||||
{
|
||||
create();
|
||||
m_backingStore = new QBackingStore(this);
|
||||
@ -68,7 +67,6 @@ RasterWindow::RasterWindow(QWindow *parent)
|
||||
bool RasterWindow::event(QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::UpdateRequest) {
|
||||
m_update_pending = false;
|
||||
renderNow();
|
||||
return true;
|
||||
}
|
||||
@ -79,10 +77,7 @@ bool RasterWindow::event(QEvent *event)
|
||||
//! [6]
|
||||
void RasterWindow::renderLater()
|
||||
{
|
||||
if (!m_update_pending) {
|
||||
m_update_pending = true;
|
||||
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
|
||||
}
|
||||
requestUpdate();
|
||||
}
|
||||
//! [6]
|
||||
|
||||
@ -99,10 +94,9 @@ void RasterWindow::resizeEvent(QResizeEvent *resizeEvent)
|
||||
//! [2]
|
||||
void RasterWindow::exposeEvent(QExposeEvent *)
|
||||
{
|
||||
if (isExposed()) {
|
||||
if (isExposed())
|
||||
renderNow();
|
||||
}
|
||||
}
|
||||
//! [2]
|
||||
|
||||
|
||||
|
@ -74,7 +74,6 @@ protected:
|
||||
|
||||
private:
|
||||
QBackingStore *m_backingStore;
|
||||
bool m_update_pending;
|
||||
};
|
||||
//! [1]
|
||||
#endif // RASTERWINDOW_H
|
||||
|
@ -2175,11 +2175,14 @@ void QWindowPrivate::deliverUpdateRequest()
|
||||
Schedules a QEvent::UpdateRequest event to be delivered to this window.
|
||||
|
||||
The event is delivered in sync with the display vsync on platforms
|
||||
where this is possible. When driving animations, this function should
|
||||
be called once after drawing has completed.
|
||||
where this is possible. Otherwise, the event is delivered after a
|
||||
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
|
||||
being delivered to the window.
|
||||
When driving animations, this function should be called once after drawing
|
||||
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
|
||||
call the application's rendering code, then call the base class
|
||||
|
Loading…
Reference in New Issue
Block a user