Translucent background not rendering correctly in Qt6 with hardware rendering but works with Software rendering
-
I have a blue main window on top of which I draw a Qt Quick Widget. I want the functionality that when the Qt Quick Widget is displayed the area of the main window turns translucent so the user focuses on the Quick widget. For that I am using these commands
m_quickWidget->setAttribute(Qt::WA_TranslucentBackground);
m_quickWidget->setClearColor(Qt::transparent);
m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);By default, the result looks like this, you can see that the background is not transparent anymore but greyed out.
But when I change the rendering to software with QQuickWindow::setSceneGraphBackend("software"); It works correctly as below.
Software rendering is slow, I want it to work with hardware rendering. Does anybody know the solution to this or have encountered this problem before with Qt6? Or is there some other way to make the background transparent which works with hardware rendering?
-
This is the code to reproduce it
#include <QQuickWidget>
#include <QQuickItem>
#include <QQmlError>
#include <QtWidgets>class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
private:
QQuickWidget *m_quickWidget;
};MainWindow::MainWindow()
: m_quickWidget(new QQuickWidget)
{
setFixedSize(QSize(800,600));m_quickWidget->setAttribute(Qt::WA_TranslucentBackground); m_quickWidget->setClearColor(Qt::transparent); QUrl source("qrc:transparent.qml"); m_quickWidget->resize(300,300); m_quickWidget->setWindowFlags(Qt::SplashScreen); m_quickWidget->setAttribute(Qt::WA_AlwaysStackOnTop); m_quickWidget->setAttribute(Qt::WA_TranslucentBackground); m_quickWidget->setClearColor(Qt::transparent); m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView); m_quickWidget->setSource(source); QWidget *centralWidget = new QWidget(); centralWidget->setStyleSheet("background-color: blue"); QHBoxLayout *layout = new QHBoxLayout(centralWidget); layout->addWidget(m_quickWidget); setCentralWidget(centralWidget);
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);MainWindow mainWindow; mainWindow.show(); return app.exec();
}
#include "main.moc"
-
@Muhammad-Hassan-Shahid
Can you try:
QQuickWindow::setSceneGraphBackend( QSGRendererInterface::OpenGL );The other options are here:
https://doc.qt.io/qt-6/qsgrendererinterface.html -
@JoeCFD When I use the QSGRendererInterface enums, compiler gives me an error that it can't convert it to Qstring and indeed there is no overload of QQuickWindow::setSceneGraphBackend for parameter of type QSGRendererInterface::GraphicsApi
-
@Muhammad-Hassan-Shahid Sorry you call this one
void QQuickWindow::setGraphicsApi(QSGRendererInterface::GraphicsApi api)The source code is here
https://codebrowser.dev/qt6/qtdeclarative/src/quick/items/qquickwindow.cpp.html
check line 3773
there are only two options: OpenVG may be the one you need.// Special cases: these are different scenegraph backends. switch (api) { case QSGRendererInterface::Software: setSceneGraphBackend(QStringLiteral("software")); break; case QSGRendererInterface::OpenVG: setSceneGraphBackend(QStringLiteral("openvg")); break; default: break; }
https://doc.qt.io/qt-6/qtquick-visualcanvas-adaptations-openvg.html
-
@Muhammad-Hassan-Shahid
check if the backend setting is correct.
from here:
QString QQuickWindow::sceneGraphBackend() -
@Muhammad-Hassan-Shahid said in Translucent background not rendering correctly in Qt6 with hardware rendering but works with Software rendering:
Could not create scene graph context for backend 'openvg' - check that plugins are installed correctly
The messages are not very helpful. Which plugins are not installed properly? You have to figure that out. I do not know it either. But you know the reasons now.
-
@Muhammad-Hassan-Shahid message in running time or compiling time?
Linux or Windows? -
@Muhammad-Hassan-Shahid The solution is to set ui->quickWidget->quickWindow()->setColor(QColor(0,0,0,0)); after the main window has been rendered to bypass this issue with hardware rendering but this problem should be solved by Qt in the future. There shouldn't be a need to do something like this in the first place.
This is the code to reproduce it
#include <QQuickWidget>
#include <QQuickItem>
#include <QQmlError>
#include <QtWidgets>class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
QQuickWidget *m_quickWidget;
};MainWindow::MainWindow()
: m_quickWidget(new QQuickWidget)
{
setFixedSize(QSize(800,600));m_quickWidget->setAttribute(Qt::WA_TranslucentBackground);
m_quickWidget->setClearColor(Qt::transparent);QUrl source("qrc:transparent.qml");
m_quickWidget->resize(300,300);
m_quickWidget->setWindowFlags(Qt::SplashScreen);
m_quickWidget->setAttribute(Qt::WA_AlwaysStackOnTop);
m_quickWidget->setAttribute(Qt::WA_TranslucentBackground);
m_quickWidget->setClearColor(Qt::transparent);
m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
m_quickWidget->setSource(source);QWidget *centralWidget = new QWidget();
centralWidget->setStyleSheet("background-color: blue");
QHBoxLayout *layout = new QHBoxLayout(centralWidget);
layout->addWidget(m_quickWidget);
setCentralWidget(centralWidget);
}int main(int argc, char **argv)
{
QApplication app(argc, argv);MainWindow mainWindow;
mainWindow.show();mainWindow.m_quickWidget->quickWindow()->setColor(QColor(0,0,0,0));
return app.exec();
}#include "main.moc"
-
-
You should open a ticket on the bug report system to make it known to Qt developers.