防止组件在 qml 窗口上变得透明

防止组件在 qml 窗口上变得透明

以下是 qml 中透明无框窗口的代码:

import QtQuick 2.2
import QtQuick.Window 2.1
import Ubuntu.Components 0.1

Window {
    MouseArea {
        anchors.fill: parent

        // move window on drag
        property real lastMouseX: 0
        property real lastMouseY: 0
        onPressed: {
          lastMouseX = mouseX
          lastMouseY = mouseY
        }
        onMouseXChanged: window1.x += (mouseX - lastMouseX)
        onMouseYChanged: window1.y += (mouseY - lastMouseY)

    }

    id: window1
    title: "transparent"
    height: 200
    width: 400
//    flags: Qt.FramelessWindowHint
    flags: Qt.Popup
    color: '#efeded'
    opacity: 0.3

    x: (Screen.width / 2) - (window1.width / 2)
    y: (Screen.height / 2) - (window1.height / 2)

    Text {
        id: text1
        text: "Hello World"
        font.bold: true
        style: Text.Raised
        font.pointSize: 24
        color: "black"
        anchors.horizontalCenter: parent.horizontalCenter
        y: 40
    }

    Button {
        id: button1
        text: "close"
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.right: parent.right
        anchors.rightMargin: 0
        y: 160

        onClicked: {
            window1.close()
        }
    }

}

结果:

在此处输入图片描述

那么是否可以防止按钮和文本变得透明?

只有窗口的背景应该是透明的。

答案1

删除“不透明度”并使用半透明颜色:

  color: Qt.rgba(0.239, 0.237, 0.237, 0.3)

http://qt-project.org/doc/qt-4.8/qml-color.html

谢谢你的提示https://stackoverflow.com/a/17244302

相关内容