summaryrefslogtreecommitdiffstats
path: root/examples/opengl/paintedwindow/paintedwindow.cpp
blob: 9b27c5691464722ffefa8485a0cfb477d34cc2b1 (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
#include "paintedwindow.h"

#include <QOpenGLContext>
#include <QOpenGLPaintDevice>
#include <QPainter>
#include <QTimer>

#include <qmath.h>

PaintedWindow::PaintedWindow()
{
    QSurfaceFormat format;
    format.setStencilBufferSize(8);
    format.setSamples(4);

    setSurfaceType(QWindow::OpenGLSurface);
    setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
    setFormat(format);

    create();

    m_context = new QOpenGLContext(this);
    m_context->setFormat(format);
    m_context->create();
}

void PaintedWindow::resizeEvent(QResizeEvent *)
{
    paint();
}

void PaintedWindow::exposeEvent(QExposeEvent *)
{
    paint();
}

void PaintedWindow::paint()
{
    m_context->makeCurrent(this);

    QPainterPath path;
    path.addEllipse(0, 0, width(), height());

    QOpenGLPaintDevice device(size());

    QPainter painter(&device);
    painter.fillRect(0, 0, width(), height(), Qt::white);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.fillPath(path, Qt::blue);
    painter.end();

    m_context->swapBuffers(this);
}