prvnsmpth
prvnsmpth

Reputation: 315

QML: How to wait until a component is re-drawn due to property change?

I have a draggable Item with a MouseArea, the Item's x and y properties are changed inside the onReleased signal handler of the MouseArea.

Item {
    id: item
    x: 10
    y: 10
    width: parent.width; height: width
    signal someSignal
    MouseArea {
        onReleased: {
            /* change x and y properties of item */ 

            /* emit signal to do further event handling */
            item.someSignal();          
        }
        drag.target: parent
    }
    ...
}

The problem is, the signal handler is invoked before the item is re-positioned (because of the change in its x and y properties) and this causes the GUI to appear to be frozen until the signal handler returns, after which the item is re-positioned to its correct location.

How can I defer the execution of the signal handler until after the item is re-drawn?

Upvotes: 2

Views: 1413

Answers (1)

Qin Peixi
Qin Peixi

Reputation: 165

I googled 'qml force redraw', but found nothing help. Add a timer can walkaround.

import QtQuick 2.0

Rectangle {
    width: 640; height: 480

    Rectangle {
        id: item
        x: 10; Behavior on x {NumberAnimation{}}
        y: 10; Behavior on y {NumberAnimation{}}
        width: 100; height: width
        color: "red"
        signal someSignal

        MouseArea {
            anchors.fill: parent
            onReleased: {
                /* change x and y properties of item */
                item.x = 100
                item.y = 100
                /* emit signal to do further event handling */
//                item.someSignal()
                timer.start()
            }
            drag.target: parent
        }
        Timer {
            id: timer
            onTriggered: item.someSignal();
        }

        onSomeSignal: {
            var a = 0
            for (var i=0; i<999999999; i++)
                a ++
            console.log("done")
        }
    }
}

Upvotes: 1

Related Questions