【问题标题】:how to create a button that moves a shape's x value up each time a button is clicked?如何创建一个每次单击按钮时向上移动形状的 x 值的按钮?
【发布时间】: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}

      }
       ]
        }

【问题讨论】:

标签: qt qml qt5 qtquick2


【解决方案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
   }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    相关资源
    最近更新 更多