使用QML淡化整个应用程序

时间:2019-04-25 13:58:52

标签: qt qml

如何在整个应用程序中淡入淡出?我尝试在以下代码中使用OpacityAnimator

import QtQuick 2.12
import QtQuick.Window 2.12

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

    OpacityAnimator
    {
        id: animator
        target: mainWindow;
        from: 0;
        to: 1;
        duration: 10000
        running: false
    }

    Rectangle
    {
        x: 0
        y: 0
        width: 200
        height: 200
        color: "black"
    }
}

但这不起作用。错误消息是:

W libFinal.so: qrc:/main.qml:15:9: Unable to assign QQuickWindowQmlImpl to QQuickItem

2 个答案:

答案 0 :(得分:4)

OpacityAnimator是错误,它指向target指向QQuickItem,而Window不是。您必须通过contentItem的内容:

OpacityAnimator
{
    id: animator
    target: mainWindow.contentItem // <---
    from: 0
    to: 1
    duration: 10000
    running: true
}

答案 1 :(得分:1)

您可以改用NumberAnimation

import QtQuick 2.12
import QtQuick.Window 2.12

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

    NumberAnimation on opacity {
        from: 0
        to: 1
        duration: 1000
        running: true
    }

    Rectangle {
        x: 0
        y: 0
        width: 200
        height: 200
        color: "black"
    }
}