如何在窗格中使用Material.elevation和Radius?

时间:2019-04-02 20:15:45

标签: qt qml material-design

我正在尝试使用添加了Material.elevation: 6的窗格,但是我想给它一个圆角的边缘,我不能同时将它们聚在一起

尝试了以下操作,但海拔不见了。

Pane {
   // ...
    Material.elevation: 6

    background: Rectangle {
        radius: 15
    }
    // ...
}

这个想法是,您可以同时保持两个方面来实现以下目标:

1 个答案:

答案 0 :(得分:1)

您必须基于source code进行覆盖:

RoundPane.qml

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.12
import QtQuick.Controls.Material.impl 2.12

Pane {
    id: control
    property int radius: 2
    background: Rectangle {
        color: control.Material.backgroundColor
        radius: control.Material.elevation > 0 ? control.radius : 0

        layer.enabled: control.enabled && control.Material.elevation > 0
        layer.effect: ElevationEffect {
            elevation: control.Material.elevation
        }
    }
}

IconPane.qml

import QtQuick 2.12
import QtQuick.Controls.Material 2.12
import QtQuick.Layouts 1.12

RoundPane {
    id: control
    property alias name: txt.text
    property alias icon: image.source
    Material.elevation: 6
    radius: 15
    RowLayout{
        anchors.fill: parent
        Image {
            id: image
            sourceSize.height: parent.height
        }
        Text {
            id: txt;
        }
    }
}

main.qml

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    IconPane{
        name: "Stack <b>Overflow</b>"
        icon: "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg"
        anchors.centerIn: parent
    }
}

enter image description here