Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

How to integrate OpenGL code with Qt Quick 2 applications (part 2)


QuickView::QuickView(QWindow *parent)
    : QQuickView(parent)
    , m_camera(new Camera(this))
    , m_renderer(new MeshRenderer(this))
{
    connect(this, &QQuickWindow::sceneGraphInitialized,
            this, &QuickView::initializeUnderlay,
            Qt::DirectConnection);

    connect(this, &QQuickWindow::beforeSynchronizing,
            this, &QuickView::synchronizeUnderlay,
            Qt::DirectConnection);

    connect(this, &QQuickWindow::beforeRendering,
            this, &QuickView::renderUnderlay,
            Qt::DirectConnection);

    connect(this, &QQuickWindow::sceneGraphInvalidated,
            this, &QuickView::invalidateUnderlay,
            Qt::DirectConnection);

    connect(m_camera, &Camera::azimuthChanged, this, &QQuickWindow::update);
    connect(m_camera, &Camera::elevationChanged, this, &QQuickWindow::update);
    connect(m_camera, &Camera::distanceChanged, this, &QQuickWindow::update);

    setClearBeforeRendering(false);

    setResizeMode(SizeRootObjectToView);
    rootContext()->setContextProperty("_camera", m_camera);
    setSource(QUrl("qrc:///qml/main.qml"));
}

void QuickView::initializeUnderlay()
{
    m_renderer->initialize();
    resetOpenGLState();
}

void QuickView::invalidateUnderlay()
{
    m_renderer->invalidate();
    resetOpenGLState();
}

void QuickView::renderUnderlay()
{
    m_renderer->render();
    resetOpenGLState();
}

void QuickView::synchronizeUnderlay()
{
    m_renderer->setAzimuth(m_camera->azimuth());
    m_renderer->setElevation(m_camera->elevation());
    m_renderer->setDistance(m_camera->distance());
}

About KDAB


15 Comments

6 - Feb - 2016

Marco Centin

9 - Feb - 2016

Giuseppe D'Angelo

class MyQuickView : public QQuickView
{
protected:
    void mousePressEvent(QMouseEvent *e) override {
        QQuickView::mousePressEvent(e); // let Qt Quick handle it first...
        if (!e->isAccepted()) // the click didn't land on anything in Qt Quick, I'll use it then
            doMyHandling(e);
    }
};

7 - Jun - 2017

milad hasanvand

11 - Jun - 2017

Giuseppe D'Angelo

16 - Dec - 2019

Huchn

7 - Apr - 2020

Reza

21 - May - 2020

Giuseppe D'Angelo

5 - May - 2020

Tomasso

21 - May - 2020

Giuseppe D'Angelo

21 - May - 2020

Dusan

21 - May - 2020

Giuseppe D'Angelo

21 - Oct - 2020

Aditya

23 - Oct - 2020

Giuseppe D'Angelo

1 - Dec - 2023

Jeff

5 - Dec - 2023

Giuseppe D'Angelo

GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer