Application Entry Point▲
int
main(int
argc, char
**
argv)
{
QGuiApplication app(argc, argv);
RasterWindow window;
window.show();
return
app.exec();
}
The entry point for a QWindow based application is the 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 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.
RasterWindow Declaration▲
#include <QtGui>
class
RasterWindow : public
QWindow
{
Q_OBJECT
public
:
explicit
RasterWindow(QWindow *
parent =
0
);
virtual
void
render(QPainter *
painter);
public
slots:
void
renderLater();
void
renderNow();
protected
:
bool
event(QEvent *
event) override
;
void
resizeEvent(QResizeEvent *
event) override
;
void
exposeEvent(QExposeEvent *
event) override
;
private
:
QBackingStore *
m_backingStore;
}
;
We first start by including the <QtGui> header. 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.
The raster window is also reused in a few other examples and adds a few helper functions, like renderLater().
RasterWindow Implementation▲
RasterWindow::
RasterWindow(QWindow *
parent)
:
QWindow(parent)
, m_backingStore(new
QBackingStore(this
))
{
setGeometry(100
, 100
, 300
, 200
);
}
In the constructor we create the backingstore and pass it the window instance it is supposed to manage. We also set the initial window geometry.
void
RasterWindow::
exposeEvent(QExposeEvent *
)
{
if
(isExposed())
renderNow();
}
Shortly after calling QWindow::show() on a created window, the virtual function 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 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.
void
RasterWindow::
resizeEvent(QResizeEvent *
resizeEvent)
{
m_backingStore