QML使用动画按坐标移动



我需要根据在ScrollView中输入的坐标移动多维数据集。

import QtQuick 2.15
import QtQuick.Window 2.14
import QtQuick3D 1.15
import QtQuick.Controls 2.14
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
visibility: "Maximized"
property int move_x: 5
property int move_y: 5
property int move_z: 5
Node{
id: standAloneScene
DirectionalLight {
ambientColor: Qt.rgba(1.0, 1.0, 1.0, 1.0)
}
Node{
Model {
id: model
source: "#Cube"
x: 0
y: 0
z: 0
materials: [
DefaultMaterial {
diffuseColor: Qt.rgba(0.053, 0.130, 0.219, 0.75)
}
]
}
}
ParallelAnimation{
id: moving
NumberAnimation {
target: model
property: "x"
from: 0
to: move_x
duration: 2000
easing.type: Easing.InOutQuad
}
NumberAnimation {
target: model
property: "y"
from: 0
to: move_y
duration: 2000
easing.type: Easing.InOutQuad
}
NumberAnimation {
target: model
property: "z"
from: 0
to: move_z
duration: 2000
easing.type: Easing.InOutQuad
}
}
OrthographicCamera {
id: cameraOrthographicFront
eulerRotation.y: 45
eulerRotation.x: -45
x: 600
y: 800
z: 600
}
}
Rectangle {
id: view
anchors.top: parent.top
anchors.left: parent.left
width: parent.width
height: parent.height
color: "#848895"
View3D {
id: isometria
anchors.fill: parent
importScene: standAloneScene
camera: cameraOrthographicFront
}
Rectangle{
id: control
color: "grey"
width: 400
anchors.top: view.top
anchors.bottom: view.bottom
anchors.right: view.right
Button {
id: start
height: control.width/4
anchors.top: control.top
anchors.left: control.left
anchors.right: control.right
text: "start"
font.pixelSize: height
onClicked: {
move.getListCoordinates(hellotext.text)
for(var i=0; i<move.get_number_crds(); i++){
move.next(i);
move_x = move.get_x()
move_y = move.get_y()
move_z = move.get_z()
moving.restart();
}
}
}
ScrollView{
id: textarea
anchors.top: start.bottom
anchors.right: control.right
anchors.left: control.left
anchors.bottom: control.bottom
TextArea{
id: hellotext
font.pixelSize: 40
}
}
}
}
}

单击该按钮时,坐标将从ScrollView中读取,并使用move类函数写入向量。然后,循环从0开始到向量中元素的数量。坐标依次传递给move_x、move_y和move_z,然后重新启动动画。移动类的实现如所示

move.h
#ifndef MOVE_H
#define MOVE_H
#include <QObject>
#include <QDebug>
#include <QVector>
struct Position
{
int x;
int y;
int z;
};
class Move: public QObject
{
Q_OBJECT
public:
Move(QObject *parent = nullptr);
private:
QVector<Position> coordinates;
Position current_position;
signals:
private:
Position StringParsing(QString st);
public slots:
void getListCoordinates(QString list_crd);
int get_number_crds();
void next(int i);
int get_x();
int get_y();
int get_z();
};
#endif // MOVE_H

move.cpp
#include "move.h"
#include <QString>
Move::Move(QObject *parent): QObject(parent) {}
Position Move::StringParsing(QString crd_st){
Position crd;
crd.x = (crd_st.mid(1, crd_st.indexOf('Y') - 1)).toInt();
crd.y = crd_st.mid(crd_st.indexOf('Y') + 1, crd_st.indexOf('Z') - crd_st.indexOf('Y') - 1).toInt();
crd.z = crd_st.mid(crd_st.indexOf('Z') + 1, crd_st.length() - crd_st.indexOf('Z') - 1).toInt();
return crd;
}
void Move::getListCoordinates(QString list_crd){
list_crd += "n";
while(list_crd.length() != 0){
QString crd =  list_crd.mid(0, list_crd.indexOf('n')+1);
list_crd.remove(0,  list_crd.indexOf('n')+1);
coordinates.push_back(StringParsing(crd));
qDebug() << crd << Qt::endl;
}
}
int Move::get_number_crds(){
return coordinates.size();
}
void Move::next(int i){
current_position = coordinates[i];
}
int Move::get_x(){
qDebug() << current_position.x << Qt::endl;
return current_position.x;
}
int Move::get_y(){
qDebug() << current_position.y << Qt::endl;
return current_position.y;
}
int Move::get_z(){
qDebug() << current_position.z << Qt::endl;
return current_position.z;
}

但是对象只移动到我记录的最后一个坐标。你能告诉我如何实现这一点吗?感谢

该问题的

А解决方案。

import QtQuick 2.15
import QtQuick.Window 2.14
import QtQuick3D 1.15
import QtQuick.Controls 2.14
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
visibility: "Maximized"
property int i: 0
property int move_x: 0
property int move_y: 0
property int move_z: 0
Node{
id: standAloneScene
DirectionalLight {
ambientColor: Qt.rgba(1.0, 1.0, 1.0, 1.0)
}
Node{
Model {
id: model
source: "#Cube"
x: 0
y: 0
z: 0
materials: [
DefaultMaterial {
diffuseColor: Qt.rgba(0.053, 0.130, 0.219, 0.75)
}
]
}
}
//        SequenceAnimation {}
SequentialAnimation{
id: myAnimation
ScriptAction{
script: {
if(i < move.get_number_crds()){
move.next(i);
move_x = move.get_x();
move_y = move.get_y();
move_z = move.get_z();
}
if(i > move.get_number_crds()){
i = 0;
myAnimation.stop();
}
}
}
ParallelAnimation{
id: moving
NumberAnimation {
target: model
property: "x"
to: move_x
duration: 2000
easing.type: Easing.InOutQuad
}
NumberAnimation {
target: model
property: "y"
to: move_y
duration: 2000
easing.type: Easing.InOutQuad
}
NumberAnimation {
target: model
property: "z"
to: move_z
duration: 2000
easing.type: Easing.InOutQuad
}
}
ScriptAction{
script: {
i++;
myAnimation.restart();
}
}
}
OrthographicCamera {
id: cameraOrthographicFront
eulerRotation.y: 45
eulerRotation.x: -45
x: 600
y: 800
z: 600
}
}
Rectangle {
id: view
anchors.top: parent.top
anchors.left: parent.left
width: parent.width
height: parent.height
color: "#848895"
View3D {
id: isometria
anchors.fill: parent
importScene: standAloneScene
camera: cameraOrthographicFront
}
Rectangle{
id: control
color: "grey"
width: 400
anchors.top: view.top
anchors.bottom: view.bottom
anchors.right: view.right
Button {
id: start
height: control.width/4
anchors.top: control.top
anchors.left: control.left
anchors.right: control.right
text: "start"
font.pixelSize: height
onClicked: {
move.getListCoordinates(hellotext.text)
myAnimation.start();
}
}
ScrollView{
id: textarea
anchors.top: start.bottom
anchors.right: control.right
anchors.left: control.left
anchors.bottom: control.bottom
TextArea{
id: hellotext
font.pixelSize: 40
}
}
}
}
}

最新更新