How to show QWidget created from a QQuickView in frameless dialog with transparent background

StackOverflow https://stackoverflow.com/questions/23315494

  •  10-07-2023
  •  | 
  •  

Question

I have a QWidget created with QWidget::createWindowContainer from a QQuickView:

QQuickView view;
view.setSource(QUrl("./Test.qml"));

QWidget *container = QWidget::createWindowContainer(&view);

The Qml is very simple:

import QtQuick 2.0
Rectangle {
    width: 50
    height: 50
    color: "blue"
    radius: 5
}

I need a translucent frameless container to show the rectangle. Trying to show the widget in a QDialog:

QDialog dialog;
QHBoxLayout layout;

dialog.setWindowFlags(Qt::FramelessWindowHint);
dialog.setAttribute(Qt::WA_TranslucentBackground);
layout.addWidget(container);
dialog.setLayout(&layout);
dialog.show();

i get nothing because it seems that transparency was propagated also to container object. If i try to add to layout instead of container widget, for example, a QPushButton, all works fine and only the button is showed. I need the same result with my QML widget. Where could be the problem? I'm using Qt 5.2.1

Was it helpful?

Solution

I doubt it could be done for a QQuickView embedded in a QWidget. But you can have a transparent QQuickView like:

QQuickView view;

view.setSurfaceType(QSurface::OpenGLSurface);

QSurfaceFormat format;
format.setAlphaBufferSize(8);
format.setRenderableType(QSurfaceFormat::OpenGL);

view.setFormat(format);
view.setColor(QColor(Qt::transparent));
view.setClearBeforeRendering(true);

view.setFlags(Qt::FramelessWindowHint);

view.setSource(QStringLiteral("./Test.qml"));

view.show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top