John Smith
John Smith

Reputation: 302

Fade in whole Application with QML

How can I fade in the whole application? I tried using OpacityAnimator in the following code:

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"
    }
}

But this doesn't work. The error message is:

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

Upvotes: 3

Views: 389

Answers (2)

augre
augre

Reputation: 2051

You can use a NumberAnimation instead:

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"
    }
}

Upvotes: 1

eyllanesc
eyllanesc

Reputation: 244162

OpacityAnimator as the error points as a target to a QQuickItem but Window is not. What you must pass the contentItem:

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

Upvotes: 4

Related Questions