如何创建一个按钮,每次单击按钮时都会向上移动形状的 x 值?
how to create a button that moves a shape's x value up each time a button is clicked?
如何创建一个按钮,每次单击按钮时,该按钮都会将形状的 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}
}
]
}
使用 states
不适合您的用例。
您可以在 onClicked
处理程序中将 Rectangle
的 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
}
}
如何创建一个按钮,每次单击按钮时,该按钮都会将形状的 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}
}
]
}
使用 states
不适合您的用例。
您可以在 onClicked
处理程序中将 Rectangle
的 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
}
}