1
votes

I have a number of objects on my screen that I change the opacity of, and to make this opacity change animated instead of instantaneous, I add this Behavior to each of the objects:

Behavior on opacity {
    NumberAnimation {
        duration: 500
        easing.type: Easing.InOutQuad
    }
}

I am attempting to subclass this (not sure if "subclass" is the correct term here). I've created a file named FadeBehavior.qml with these contents:

import QtQuick 2.3
import QtQuick.Controls 1.3 as Controls

Behavior on opacity {

    id: fadeBehavior

    NumberAnimation {
        duration: 500
        easing.type: Easing.InOutQuad
    }

}

Then, instead of adding the Behavior to each object, I'm adding:

FadeBehavior { }

But this is not working (sorry I can't add more information than "not working" - this is an inherited app and I have not been able to run it in debug mode; when I make a mistake in my QML file, all that happens is that my window comes up as a blank one-inch square).

It seems as if the on opacity in the first line is the part Qt doesn't like. In FadeBehavior.qml on opacity is underlined in red, expecting token {. Is there some other syntax for specifying the name of the property the Behavior is attached to?

1
You can't really do this, Behavior is a "special" QML element, even its syntax is an exception in the QML language paradigm. I personally don't like that, especially the lack of an alternative way to specify the target, I have found this to be very liming on many occasions.dtech

1 Answers

0
votes

Here is one work around that would work.

Subclass all of the standard types that you need (Rectangle, Item, Image, etc.) like this

Item {
    Behavior on opacity {
        NumberAnimation {
            duration: 500
            easing.type: Easing.InOutQuad
        }
    }
}

Save this as MyItem.qml and change every Item to MyItem.