Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear QML anchor

Tags:

I have a MouseArea that I want to start off centered and then have an absolute position once the up/down/left/right keys are pressed. My problem is that I don't know how to clear the anchor on the MouseArea so that I can specify an absolute position:

import QtQuick 2.0 import QtQuick.Window 2.0  Window {     id: screen     width: 360     height: 360     visible: true      Rectangle {         anchors.fill: parent          states: [             State {                 name: "moved"                 AnchorChanges {                     target: mouseArea                     anchors.bottom: undefined                     anchors.left: undefined                     anchors.right: undefined                     anchors.top: undefined                 }             }         ]          MouseArea {             id: mouseArea             anchors.centerIn: parent             width: 250             height: 250             focus: true             onClicked: console.log("clicked!")             onPositionChanged: console.log("position changed!")              function moveMouseArea(x, y) {                 mouseArea.x += x;                 mouseArea.y += y;                 mouseArea.state = "moved";                 mouseAreaPosText.text = 'Mouse area was moved... new pos: '                     + mouseArea.x + ', ' + mouseArea.y;             }              Keys.onPressed: {                 if (event.key === Qt.Key_Up)                     moveMouseArea(0, -1);                 if (event.key === Qt.Key_Down)                     moveMouseArea(0, 1);                 if (event.key === Qt.Key_Left)                     moveMouseArea(-1, 0);                 if (event.key === Qt.Key_Right)                     moveMouseArea(1, 0);             }              Rectangle {                 anchors.fill: parent                 border.width: 2                 border.color: "black"                 color: "transparent"             }              Text {                 id: mouseAreaPosText                 anchors.centerIn: parent             }         }     } } 

At first I just tried setting mouseArea.anchors to undefined but got an error about anchors being a read-only property. I then discovered AnchorChanges, but I can't find a way to remove/clear the anchor; setting anchors.bottom etc. to undefined doesn't work.

like image 673
Mitch Avatar asked Jun 03 '12 11:06

Mitch


People also ask

What are anchors in QML?

QML provides anchors to position different items relative to each others. For each item, there are 7 imaginary lines called anchor lines. An item can be placed in these anchor lines relative to another item(parent or any sibling).

What is opacity in QML?

Opacity is specified as a number between 0.0 (fully transparent) and 1.0 (fully opaque). The default value is 1.0. When this property is set, the specified opacity is also applied individually to child items. This may have an unintended effect in some circumstances.

What is anchor in Qt?

The Qt Quick anchoring system allows you to define relationships between the anchor lines of different items. For example, you can write: Rectangle { id: rect1; ... } Rectangle { id: rect2; anchors.

How do you add margins in QML?

If an Item is affected by a Layout then you must use Layout. margins if you want to set all the margins to the same value, but if you want to set a different margin in each direction you must use Layout. leftMargin, Layout. topMargin, Layout.


1 Answers

According to docs, setting an anchor attribute to undefined should work. I don't quite get why AnchorChanges did not allow to set anchors.centerIn, but you can workaround it in your moveMouseArea function:

function moveMouseArea(x, y) {     mouseArea.anchors.centerIn = undefined; // <-- reset anchor before state change     mouseArea.pos.x += x;     mouseArea.pos.y += y;     mouseArea.state = "moved";     mouseAreaPosText.text = 'Mouse area was moved... new pos: '         + mouseArea.pos.x + ', ' + mouseArea.pos.y; } 
like image 147
sergk Avatar answered Sep 20 '22 15:09

sergk