24

中央から始めて、上/下/左/右のキーが押されると絶対位置が必要な MouseArea があります。私の問題は、絶対位置を指定できるように、MouseArea のアンカーをクリアする方法がわからないことです。

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
            }
        }
    }
}

最初はに設定しようとしmouseArea.anchorsましたが、読み取り専用プロパティであるundefinedというエラーが発生しました。anchorsその後、AnchorChanges を発見しましたが、アンカーを削除/クリアする方法が見つかりません。への設定anchors.bottom等がundefinedうまくいきません。

4

2 に答える 2

27

docsによると、アンカー属性をに設定するundefinedとうまくいくはずです。AnchorChangesset を許可しなかった理由はよくわかりません が、関数anchors.centerInで回避できます。moveMouseArea

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;
}
于 2012-06-03T22:53:21.360 に答える
1

助けてくれてありがとう。状態内で undefined を設定すると機能することがわかりました(動作によってエラーが発生しないことだけを意味する場合)が、要素がさらに別の状態に移動すると、アンカーが魔法のように(そして非常にイライラして)戻ります。これは、最終状態ですべてのアンカーを未定義に設定した場合でも発生します。ただし、前述のように、状態を変更する前に関数で undefined を設定すると、うまく機能します。私の場合、onPressed の mouseArea に設定しました。

                onPressed: {
                plotWindow04Frame.anchors.bottom = undefined
                plotWindow04Frame.anchors.left = undefined
                plotWindow04Frame.state = "inDrag"
            }

onReleased でアンカーについて言及する必要はなく、単に次の状態であることがわかりました。onReleased: { plotWindow04Frame.state = "ドロップ" }

また、最終的な「ドロップ」状態では、アンカーについても言及されておらず、不透明であることに注意してください。

    states: [
    State {
        name: "inDrag"
        PropertyChanges {
            target: plotWindow04Frame
            opacity: .5
        }
    },
    State {
        name: "dropped"
        PropertyChanges {
            target: plotWindow04Frame
            opacity: 1
        }
    }

 ]

    transitions: Transition {
        NumberAnimation { properties: "opacity"; duration:200 }
    }
}

(ここでの考え方は、これらのプロット ウィンドウはドラッグ中に半透明 (不透明度: 0.5) になり、ユーザーがドロップすると不透明 (不透明度: 1) に戻るというものでした)

素晴らしいのは、プロットウィンドウの「長方形」が最初は GUI の下部に固定されていることですが、ユーザーがそれらを拾うと、好きな場所に配置できます。

于 2012-11-15T23:46:00.867 に答える