JustWe
JustWe

Reputation: 4484

QML: How to reset item property to defaut

Is there any way to reset the property of item?

For example:

Item{
 id: test
 x: 10; y: 10
}

Then i set the x,y to (100,100). Without change the x,y to (10,10) back. Any function like this test.reset().

My problem is: i drag an item, if it not drop in the correct area, i wanna the item return back automatically.

Upvotes: 3

Views: 4966

Answers (2)

air-dex
air-dex

Reputation: 4180

Item {
    id: test
    x: 10
    y: 10

    function reset() {
        x = 10;
        y = 10;
    }
}

What else?

Upvotes: 1

Mitch
Mitch

Reputation: 24406

In general, setting a property to undefined causes it to be reset. An example of this is Item's anchors grouped properties:

To clear an anchor value, set it to undefined.

Qt's property system has the concept of a resettable property:

A RESET function is optional. It is for setting the property back to its context specific default value. e.g., QWidget::cursor has the typical READ and WRITE functions, QWidget::cursor() and QWidget::setCursor(), and it also has a RESET function, QWidget::unsetCursor(), since no call to QWidget::setCursor() can mean reset to the context specific cursor. The RESET function must return void and take no parameters.

However, for your specific problem, you probably want to take a look at the Drag and Drop example. That demonstrates how to return an item to its original position.

Upvotes: 4

Related Questions