如何创建一个按钮,每次单击按钮,该按钮会将形状的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}
}
]
}
答案 0 :(得分:0)
使用states
不适合您的用例。
您只需在Rectangle
处理程序中将onClicked
的y减小10。
它很简单:
import QtQuick 2.11
import QtQuick.Controls 2.4
Item
{
Rectangle
{
id: rectangle333
color:"#ef5350"
y: 50
width: parent.width
height: 1
}
Button
{
anchors.centerIn: parent
text: "Move line up"
onClicked: rectangle333.y-=10
}
}