我必须制作一个由C ++控制的机械计数器。我是从包含数字(0,1,2,3,4,5,6,7,8,9,0)的图像中完成的。一次只能看到一位数字。我希望此计数器仅沿一个方向(向上)变化,然后我想到了这一理论:如果新数字小于旧数字,我首先转到最后一个零,然后禁用动画,转到第一个零,启用动画,最后转到所需的编号。但这行不通。它立即移到第一个零,然后与动画一起移到所需的数字。这是代码:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
id: mainWindow
visible: true
visibility: "Maximized"
property int digit0Y: 0
property bool anim0Enabled: true
Item {
id: root
visible: true
anchors.fill: parent
Rectangle {
id: container
width: 940; height:172
anchors.centerIn: parent
clip: true
color: "black"
NumberElement {
id: digit0
y: mainWindow.digit0Y; x: 0
animationEnabled: anim0Enabled
}
}
}
}
NumberElement.qml:
import QtQuick 2.0
Rectangle {
id: root
property bool animationEnabled: true
width: 130; height: 1892
color: "transparent"
Behavior on y {
enabled: root.animationEnabled
SmoothedAnimation { velocity: 200; duration: 1500; alwaysRunToEnd: true }
}
Image {
id: digits
source: "http://s30.postimg.org/6mmsfxdb5/cifre_global.png"
}
}
编辑:
#include <QQmlComponent>
#include <QGuiApplication>
#include <QThread>
#include <QQmlApplicationEngine>
#include <QQmlProperty>
#include <QDebug>
int number = 0;
int oldDigit = 0;
void set(QObject *object, int number) {
int newDigit = number%10;
if (newDigit < oldDigit) {
QQmlProperty::write(object, "digit0Y", -1720);
QQmlProperty::write(object, "anim0Enabled", false);
QQmlProperty::write(object, "digit0Y", 0);
QQmlProperty::write(object, "anim0Enabled", true);
}
QQmlProperty::write(object, "digit0Y",newDigit*(-172));
oldDigit = newDigit;
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlEngine engine;
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
if (component.status() == QQmlComponent::Error) {
qWarning() << component.errorString();
return 1;
}
QObject *object = component.create();
set(object, 9);
//QThread::msleep(1000);
set(object, 1);
return app.exec();
}
通常,一个单独的类负责设置与某些事件相关的数字,但我尝试简化以演示我的问题。在上面的示例中,数字变为1,并且不关心set(object, 9)
。那是我的问题。
您需要等待第一个动画完成后才能开始第二个动画。您可能会想,“但是我设置alwaysRunToEnd
为true
...”,但这对这里没有帮助:
此属性保存动画在停止时是否应运行完成。
如果为true,则动画将在停止时完成其当前迭代-通过将running属性设置为false或调用stop()方法。
当前正在发生的事情是,您将其设置9
为数字,以告诉动画它应该开始制作动画,但是接下来您给出的下一条指令是将其设置1
为数字,这告诉动画停止正在做的事情并为这一新变化设置动画。 。
通常,从QML处理Qt Quick动画更加容易。
我也建议PathView
使用此特定用例,因为它可能使您更轻松地实现所追求的目标。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句