QML學習之路03: 滑鼠點選視窗實現簡單的旋轉動畫
阿新 • • 發佈:2019-02-09
main.cpp
#include <QGuiApplication>
//#include <QQmlApplicationEngine>
#include <QQuickView>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
// QQmlApplicationEngine engine;
// engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
// if (engine.rootObjects().isEmpty())
// return -1;
QQuickView *view = new QQuickView();
view->setSource(QUrl("qrc:/animation.qml"));
view->show(); //實現快速預覽
return app.exec();
}
animation.qml
import QtQuick 2.9
Image{
id: root
source: "background.png" //注意: 載入圖片圖示資源前, 需要將這些資源加入qrc檔案, 否則執行時無法得到想要的結果
property int padding: 100 //自定義屬性
property int duration: 400
property bool running: false
Image{
id: box
x: root.padding
y: (root.height-height)/2
source: "rocket.png"
NumberAnimation on x{ //沿著x座標軸
to: root.width - box.width - root.padding
duration : root.duration
running: root.running
}
RotationAnimation on rotation{ //旋轉動畫
to: 360
duration: root.duration
running: root.running
}
}
MouseArea{ //指定滑鼠響應區域
anchors.fill: parent
onClicked: root.running = true
}
}
執行結果為: