Question

Is there a way to make the window of a qml application transparent?

I'm looking for a detailed description on how to draw simple shapes with qml while making the window of the application transparent, as well as the background. A working source code demo would be awesome.

Was it helpful?

Solution 3

I finally found a simple way to draw a couple of red/blue rectangles while leaving the window transparent.

enter image description here

draw_rectangles.qml

import Qt 4.7

Item {
     Rectangle {
         opacity: 0.5
         color: "red"
         width: 100; height: 100
         Rectangle {
             color: "blue"
             x: 50; y: 50; width: 100; height: 100
         }
     }
 }

win.cpp:

#include <QApplication>
#include <QDeclarativeView>
#include <QMainWindow>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMainWindow window;

    QDeclarativeView* v = new QDeclarativeView;
    window.setCentralWidget(v);

    v->setSource(QUrl::fromLocalFile(("draw_rectangles.qml")));   

    window.setStyleSheet("background:transparent;");
    window.setAttribute(Qt::WA_TranslucentBackground);
    window.setWindowFlags(Qt::FramelessWindowHint);
    window.show();

    return app.exec();
}

win.pro:

TEMPLATE += app
QT += gui declarative
SOURCES += win.cpp

Save these files to the same directory and execute qmake followed by make to compile the application.

OTHER TIPS

Here is a simple example:

main.cpp:

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDeclarativeView>

class MainWindow : public QDeclarativeView
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QDeclarativeView(parent)
{
    // transparent background
    setAttribute(Qt::WA_TranslucentBackground);
    setStyleSheet("background:transparent;");

    // no window decorations
    setWindowFlags(Qt::FramelessWindowHint);

    // set QML file
    setSource(QUrl("main.qml"));
}

MainWindow::~MainWindow()
{
}

main.qml

import QtQuick 1.0

Rectangle {
    id: root

    width: 250
    height: 250

    // completely transparent background
    color: "#00FFFFFF"

    border.color: "#F00"
    border.width: 2

    Rectangle {
        id: ball

        height: 50; width: 50
        x: 100

        color: "#990000FF"
        radius: height / 2
    }

    SequentialAnimation {
        running: true; loops: Animation.Infinite
        NumberAnimation { target: ball; property: "y"; to: root.height - ball.height; duration: 1000; easing.type: Easing.OutBounce }
        PauseAnimation { duration: 1000 }
        NumberAnimation { target: ball; property: "y"; to: 0; duration: 700 }
        PauseAnimation { duration: 1000 }
    }
}

transp-qml.pro

QT += core gui declarative

TARGET = transp-qml
TEMPLATE = app


SOURCES += main.cpp\
           mainwindow.cpp

HEADERS += mainwindow.h

OTHER_FILES += main.qml

screenshot of result:

screenshot

As of at least Qt 5.3 you don't need anything as elaborate as in the previous answers:

Window {
    flags: Qt.ToolTip | Qt.FramelessWindowHint | Qt.WA_TranslucentBackground

    color: "#00000000"

Job done. (You might want to change ToolTip. I'm using it because I'm making tooltips.)

I'm using Qt 5.3 with both C++ and QML and found that I needed to call QQuickWindow::setDefaultAlphaBuffer. This has to be done before creating the first QQuickWindow, so in C++, not QML. The window color and flags could probably be set in QML, but I opted to put all the code for winow transparency in one place like so:

QQuickView view;
QQuickWindow::setDefaultAlphaBuffer(true);
view.setColor(Qt::transparent);
view.setFlags(m_View.flags() | 
              static_cast<Qt::WindowFlags>(Qt::WA_TranslucentBackground));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top