用弹出窗口蒙上阴影

时间:2018-07-27 08:33:12

标签: qt qml

这是我的MWE:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = BeerRestBase.Config.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        properties = "server.port=0")
public abstract class BeerRestBase {

    // your tests go here

    // in this config class you define all controllers and mocked services
@Configuration
@EnableAutoConfiguration
static class Config {

    @Bean
    PersonCheckingService personCheckingService()  {
        return personToCheck -> personToCheck.age >= 20;
    }

    @Bean
    ProducerController producerController() {
        return new ProducerController(personCheckingService());
    }
}

}

我要执行的操作是通过下面的窗口的弹出窗口蒙上阴影。提供的代码不起作用,我也不知道如何解决。矩形也可以覆盖整个Popup并投射阴影,但我认为在这种情况下,阴影会投射在Popup上,而不是在窗口下方。

编辑

这在给出答案后有效:

import QtQuick.Window 2.2
import QtQuick.Controls 2.1
import QtQuick 2.0
import QtGraphicalEffects 1.0

ApplicationWindow
{
    id: window
    width: 400
    height: 400
    visible: true

    Button
    {
        text: "Open"
        onClicked: popup.open()
    }

    Popup
    {
        id: popup

        x: 100
        y: 100
        width: 200
        height: 300
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

        Rectangle
        {
            id: popRect;
            color: "red";

            //anchors.centerIn: parent
            width: parent.width;
            height: parent.height;
        }
    }

    DropShadow
    {
        id: theShadow;

        visible: true;
        anchors.fill: popup;
        horizontalOffset: 0;
        verticalOffset: 5;
        radius: 3;
        samples: 7;
        color: "black";
        source: popup;
    }
}

1 个答案:

答案 0 :(得分:1)

前几天,我在同样的情况下苦苦挣扎。这是我的解决方案:

Popup {
    id: popup

    background: Corner{   //custom control I made, which has a slanted corner
        id: backCorner
    }

    DropShadow {
        width: popup.width
        height: popup.height
        x: -leftPadding
        y: -topPadding

        source: backCorner
        color: "#80000000"
        verticalOffset: 10
        samples: 30
    }
}

这有点笨拙,因为您想使用锚定功能,但是我认为它不再能确定弹出窗口的正确大小(我想我也找到了解决方法,但是仍然必须尝试,会更新(如果可以)

修改

是的,另一种解决方案是将背景与DropShadow结合起来

background: Item {
    IOSCorner{
        anchors.fill: parent
        id: backCorner
    }

    DropShadow {
        anchors.fill: parent
        source: backCorner
        color: "#80000000"
        verticalOffset: 10
        samples: 30
    }
}

看起来弹出窗口的大小计算现在可以处理阴影了。如果您查看弹出窗口的代码,您可能会理解; DropShadow成为子级,并且由于它是第一个子级,它将导致尺寸计算

implicitWidth: Math.max(background ? background.implicitWidth : 0, contentWidth + leftPadding + rightPadding)
implicitHeight: Math.max(background ? background.implicitHeight : 0, contentHeight + topPadding + bottomPadding)

contentWidth: contentItem.implicitWidth || (contentChildren.length === 1 ? contentChildren[0].implicitWidth : 0)
contentHeight: contentItem.implicitHeight || (contentChildren.length === 1 ? contentChildren[0].implicitHeight : 0)