【发布时间】:2019-05-05 22:24:07
【问题描述】:
如何创建一个按钮,每次单击按钮时将形状的 y 值向上移动 10?这是我到目前为止所拥有的,但它只会将 y 值推高一次。我有一个 QML 文件、一个 cpp 文件和一个 .pro 文件,所有三件事的代码都在下面。
Main.cpp 文件:
#include <QGuiApplication>
#include <QQuickView>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl("qrc:/qml/MyRectangle/MyRectangle.qml"));
if (!view.errors().isEmpty())
return -1;
view.show();
app.exec();
}
MyRectangle.qml 文件:
// This is the shape I want to move up the y axis
Rectangle {
id: rectangle333
x: 461
y: 187
width: 731
height: 1
color:"#ef5350"
z: 2
}
// This is the "button" I want the user to press, that would trigger the movement.
Rectangle {
id: myRect
width: 18
height: 18
color: "transparent"
x: 232
y: 250
z:132
MouseArea {
id: mouseArea5
anchors.fill: parent
onClicked: myRect.state == 'clicked' ? myRect.state = "" :
myRect.state = 'clicked';
}
states: [
State {
name: "clicked"
// Rectangle333 is the id of the shape that I want to move up the y axis.
PropertyChanges { target: rectangle333; y:1}
}
]
}
【问题讨论】:
-
什么是
rectangle333? -
是我要移动的矩形的id。