1

is there a way to use behavior from outside the item ? (QtQuick designer doesn't support behavior).

say I have a rectangle defined in From.ui.qml with then id rec, in the file Form.qml I want to assign a behavior on x property of rec, how can I do it.?

Houss_gc
  • 727
  • 5
  • 27

1 Answers1

4
  1. Step: Expose the object with the property you want to change

enter image description here

This will create a property alias in the ui.qml-file.

// NewFormular.ui.qml

import QtQuick 2.4

Item {
    width: 400
    height: 400
    property alias rectangle1: rectangle1

    Rectangle {
        id: rectangle1
        x: 77
        y: 69
        width: 200
        height: 200
        color: "#ffffff"
    }
}
  1. Step: Add the Behavior

//New.qml

import QtQuick 2.4

NewFormular {
    Behavior on rectangle1.x {
        NumberAnimation { duration: 500 }
    }

    Timer {
        running: true
        interval: 1000
        repeat: true
        onTriggered: rectangle1.x = (rectangle1.x + 500) % 600
    }
}
  1. Step: Instantiate it in your main.qml

//main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
    id: window
    width: 800
    height: 600
    visible: true
    color: 'grey'
    New {
        anchors.fill: parent
    }
}

If you want to hide it again, you can wrap it in a Item again:

//New.qml v2

import QtQuick 2.4
Item {
    id: root
    NewFormular {
        anchors.fill: parent
        Behavior on rectangle1.x {
            NumberAnimation { duration: 500 }
        }

        Timer {
            running: true
            interval: 1000
            repeat: true
            onTriggered: rectangle1.x = (rectangle1.x + 500) % 600
        }
    }
}
  • 1
    Okey I see, you suggest that I create a normal QtQuick designer item that wrap it in another Item and expose what I want a properties. – Houss_gc Aug 09 '17 at 13:47
  • Yes, as the [documentatio](http://doc.qt.io/qtcreator/creator-quick-ui-forms.html) states, that `Behavior`s are not supported in `ui.qml`-files, the only option is, to add them externally - for which you need to expose the properties at least in the `ui.qml`-file. – derM - not here for BOT dreams Aug 09 '17 at 13:49
  • Ok, It's the only solution, I will stick to it. Thank you very much for your help – Houss_gc Aug 09 '17 at 13:51