首页 > 解决方案 > QML:按钮在使用 Material Style 的图像上变得透明

问题描述

我希望我的应用程序使用图像作为背景。所以我使用这段代码:

在我的应用程序窗口中:

Image {
    id: bkgImage
    source: "qrc:/images/bkg.jpg"
    anchors.centerIn: parent
}

添加按钮

Button {
    id: btnAsistencia
    text: qsTr("ASISTENCIA")
    font.pixelSize: fhButttonTextSize
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.top: btnInscripcion.bottom
    anchors.topMargin: parent.height*0.1
}

上面的代码是第二个按钮,但我之前添加了一个完全相同的方式。这是结果:

在此处输入图像描述

第二个按钮在图像上变得透明。我怎样才能防止这种情况?

标签: qtqml

解决方案


Material.background您可以使用附加属性设置按钮的背景颜色:

import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0

ApplicationWindow {
    id: window
    width: 400
    height: 440
    visible: true
    color: "red"

    Button {
         text: qsTr("ASISTENCIA")
         Material.background: "#666"
    }
}

默认颜色具有一定的透明度:

http://code.qt.io/cgit/qt/qtquickcontrols2.git/tree/src/imports/controls/material/qquickmaterialstyle.cpp#n862


推荐阅读