如何使用Qt4.7.4(QDeclarativeView或QWidget)在Ubuntu上显示透明窗口?

时间:2017-10-18 08:38:31

标签: qt qwidget

嘿哥们,  我想将透明窗口显示为触摸窗口(对于gst视频播放器,此视频播放器有三个窗口:1,黑色背景; 2,视频图片; 3,触摸窗口)。 我们选择Qt4.7.4,在测试触摸窗口之前一切似乎都没问题。我测试了QDeclarativeView和QWidget,但它们都因为白色背景而失败,我的代码如下:

////main.cpp    


    QDeclarativeView viewer;
    //viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setAttribute(Qt::WA_TranslucentBackground);

    viewer.setStyleSheet("background:transparent;");



    viewer.setBackgroundBrush(QColor(Qt::transparent));
    //viewer.setBackgroundRole();
    viewer.setSource(QUrl::fromLocalFile("qml/SanLiTest/main.qml"));
 //viewer.setMainQmlFile(QLatin1String("qml/SanLiTest/main.qml"));
    viewer.show();
    //viewer.showExpanded();


    //QWidget
    QWidget* touch = new QWidget();
    touch->setWindowOpacity(1);
    touch->setWindowFlags(Qt::FramelessWindowHint);
    touch->setAttribute(Qt::WA_TranslucentBackground);
    touch->show();
/////////////////////////////////////////////////


main.qml

import QtQuick 1.1

Rectangle {
    //width: Screen.desktopAvailableWidth
    //height: Screen.desktopAvailableHeight
    width: 1024
    height: 600

    color: "#00000000"

    //opacity:0


    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log("onClicked:", mouseX, mouseY);
        }
    }




//    Component.onCompleted: {

//    }


}

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这段代码:

QDeclarativeView viewer;
viewer.setProperty("windowOpacity", "value here");

您可以选择0.0到1.0之间的任何值