如何使一个透明的窗口与Qt Quick? [英] How to make a transparent window with Qt Quick?

查看:265
本文介绍了如何使一个透明的窗口与Qt Quick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 qml 应用程式的视窗透明?

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

我在寻找 如何使用qml绘制简单的形状,同时使应用程序的窗口透明,以及背景。

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.

推荐答案

我终于找到了一个简单的方法来绘制几个红色/蓝色矩形

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

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

将这些文件保存到同一目录并执行 qmake ,后跟 make 来编译应用程序。

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

这篇关于如何使一个透明的窗口与Qt Quick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆