From 70a919d4d97619ad12be1a315ecfa81af5367406 Mon Sep 17 00:00:00 2001 From: Dom Chen Date: Tue, 5 Dec 2023 19:29:41 +0800 Subject: [PATCH] Fix the qt demo on windows. (#1955) --- qt/src/PAGView.cpp | 58 +++++++++++++++++++++++++++------------------- qt/src/PAGView.h | 5 ++++ qt/src/main.cpp | 5 ++-- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/qt/src/PAGView.cpp b/qt/src/PAGView.cpp index d3e9f08afd..215b55ea48 100644 --- a/qt/src/PAGView.cpp +++ b/qt/src/PAGView.cpp @@ -71,16 +71,29 @@ PAGView::~PAGView() { delete pagPlayer; } +void PAGView::onCreateDrawable(QOpenGLContext* context) { + if (drawable == nullptr) { + drawable = GPUDrawable::MakeFrom(this, context); + drawable->moveToThread(renderThread); + } +} + +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) + +void PAGView::geometryChange(const QRectF& newGeometry, const QRectF& oldGeometry) { + if (newGeometry == oldGeometry) { + return; + } + QQuickItem::geometryChange(newGeometry, oldGeometry); + onSizeChanged(); +} + void PAGView::handleWindowChanged(QQuickWindow* window) { if (drawable != nullptr || window == nullptr) { return; } -#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) auto openglContext = reinterpret_cast(window->rendererInterface()->getResource( window, QSGRendererInterface::OpenGLContextResource)); -#else - auto openglContext = window->openglContext(); -#endif if (openglContext != nullptr) { onCreateDrawable(openglContext); } else { @@ -90,40 +103,37 @@ void PAGView::handleWindowChanged(QQuickWindow* window) { void PAGView::handleOpenglContextCreated() { disconnect(window(), SIGNAL(sceneGraphInitialized()), this, SLOT(handleOpenglContextCreated())); -#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) auto openglContext = reinterpret_cast(window()->rendererInterface()->getResource( window(), QSGRendererInterface::OpenGLContextResource)); -#else - auto openglContext = window()->openglContext(); -#endif onCreateDrawable(openglContext); } -void PAGView::onCreateDrawable(QOpenGLContext* context) { - if (drawable == nullptr) { - drawable = GPUDrawable::MakeFrom(this, context); - drawable->moveToThread(renderThread); - } -} - -#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) +#else -void PAGView::geometryChange(const QRectF& newGeometry, const QRectF& oldGeometry) { +void PAGView::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) { if (newGeometry == oldGeometry) { return; } - QQuickItem::geometryChange(newGeometry, oldGeometry); + QQuickItem::geometryChanged(newGeometry, oldGeometry); onSizeChanged(); } -#else - -void PAGView::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) { - if (newGeometry == oldGeometry) { +void PAGView::handleWindowChanged(QQuickWindow* window) { + if (drawable != nullptr || window == nullptr) { return; } - QQuickItem::geometryChanged(newGeometry, oldGeometry); - onSizeChanged(); + if (window->openglContext() != nullptr) { + onCreateDrawable(window->openglContext()); + } else { + connect(window, SIGNAL(openglContextCreated(QOpenGLContext*)), this, + SLOT(handleOpenglContextCreated(QOpenGLContext*))); + } +} + +void PAGView::handleOpenglContextCreated(QOpenGLContext* context) { + disconnect(window(), SIGNAL(openglContextCreated(QOpenGLContext*)), this, + SLOT(handleOpenglContextCreated(QOpenGLContext*))); + onCreateDrawable(context); } #endif diff --git a/qt/src/PAGView.h b/qt/src/PAGView.h index e04e46efe7..11cb58091a 100644 --- a/qt/src/PAGView.h +++ b/qt/src/PAGView.h @@ -59,8 +59,13 @@ class PAGView : public QQuickItem { Q_SLOT void handleWindowChanged(QQuickWindow* window); +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) Q_SLOT void handleOpenglContextCreated(); +#else + Q_SLOT + void handleOpenglContextCreated(QOpenGLContext* context); +#endif friend class RenderThread; }; diff --git a/qt/src/main.cpp b/qt/src/main.cpp index 35a99cffe2..0aef3f6e3d 100644 --- a/qt/src/main.cpp +++ b/qt/src/main.cpp @@ -28,11 +28,8 @@ #include "qobject.h" int main(int argc, char* argv[]) { - PAGViewer app(argc, argv); QApplication::setApplicationName("PAGViewer"); QApplication::setOrganizationName("pag.art"); - QApplication::setWindowIcon(QIcon(":/images/window-icon.png")); - QSurfaceFormat defaultFormat = QSurfaceFormat(); defaultFormat.setRenderableType(QSurfaceFormat::RenderableType::OpenGL); defaultFormat.setVersion(3, 2); @@ -45,6 +42,8 @@ int main(int argc, char* argv[]) { QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); #endif + PAGViewer app(argc, argv); + QApplication::setWindowIcon(QIcon(":/images/window-icon.png")); qmlRegisterType("PAG", 1, 0, "PAGView"); auto rootPath = QApplication::applicationDirPath(); rootPath = QFileInfo(rootPath + "/../../").absolutePath();