QOpenGLWidget doesn't work with QQuickWidget
-
Hello.
I am using Qt 6.5.0 under Windows 10.
I am trying to use OpenGL and QML at the same time.
So, I made a simple test project based on the Qt Quick Widget Example in Qt Creator 10.0.1.
(C:\Qt\Examples\Qt-6.5.0\quick\quickwidgets\quickwidget)What I did is adding MyWidget class and calling addSubWindow(my_widget);
QUrl source("qrc:quickwidget/rotatingsquare.qml"); connect(m_quickWidget, &QQuickWidget::statusChanged, this, &MainWindow::quickWidgetStatusChanged); connect(m_quickWidget, &QQuickWidget::sceneGraphError, this, &MainWindow::sceneGraphError); m_quickWidget->resize(300,300); m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView ); m_quickWidget->setSource(source); centralWidget->addSubWindow(m_quickWidget); MyWidget* my_widget = new MyWidget(this); QSurfaceFormat format_; format_.setDepthBufferSize(24); format_.setStencilBufferSize(8); format_.setVersion(3, 2); format_.setProfile(QSurfaceFormat::CoreProfile); my_widget->setFormat(format_); my_widget->setMinimumSize(300,300); centralWidget->addSubWindow(my_widget); setCentralWidget(centralWidget);
When I try to create QOpenGLWidget and QQuickWidget at the same time, I get below error message and OpenGL Window is not rendered properly.
The top-level window is not using OpenGL for composition, 'D3D11' is not compatible with QOpenGLWidget
///centralWidget->addSubWindow(m_quickWidget);
The QOpenGLWidget is correctly rendered if I comment out QQuickWidget.
Could you let me know how to use both widgets at the same time?#include <QOpenGLWidget> #include <QOpenGLFunctions> #include <QtOpenGL> class MyWidget : public QOpenGLWidget, protected QOpenGLFunctions { public: MyWidget(QWidget* parent = nullptr); protected: void initializeGL() override; void resizeGL(int w, int h) override; void paintGL() override; }; MyWidget::MyWidget(QWidget* parent) :QOpenGLWidget(parent) { } void MyWidget::initializeGL() { initializeOpenGLFunctions(); glClearColor(1.0f, 0.1f, 1.0f, 1.0f); } void MyWidget::resizeGL(int w, int h) { } void MyWidget::paintGL() { }
-
@Hanabear This might be related to this post
I had a similar situation where I wanted to use a QWebEngineView and pyqtgraph GLViewWidget in the same application.
The line suggested in the other post solved this for me.
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
or in my case (PyQt6)
from PyQt6.QtQuick import QQuickWindow, QSGRendererInterface
QQuickWindow.setGraphicsApi(QSGRendererInterface.GraphicsApi.OpenGL)
-
@Ioneater Thank you for your information.
I just created dummy QQuickWindow and called setGraphicsApi as you suggested.
By calling graphicsApi before creating the first QQuickWidget, my problem has been solved.I also found a straightforward method not using dummy QQuickWindow by setting the QSG_RHI_BACKEND environment variable.
Have a nice day.
-