QT动画制作(三十二)
效果:还是新建一个quick application 项目,然后删去MainForm.ui.qml文件,把main.qml文件修改为:
import QtQuick 2.5
import Test 1.0
Rectangle{
Test {
id: aPieChart
anchors.centerIn: parent
width: 100; height: 100
color: "green"
}
width:200
height: 200
color:"red"
RotationAnimator on rotation {
from:0
to:360
duration: 1000
loops:Animator.Infinite
}
}
main.cpp文件修改为:
#include <QtGui/QGuiApplication>
#include <QtQuick/QQuickView>
#include <QtQml>
#include "test.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc,argv);
qmlRegisterType<test>("Test", 1, 0, "Test");//注册test类为qml对象,第一个参数是对象名,第二和三个是版本号
QQuickView viewer;
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
viewer.setSource(QUrl("qrc:/main.qml"));
viewer.show();
return app.exec();
}
然后在总目录选添加新文件,添加C++ class文件名为test的文件,系统会自动生成test.h和test.cpp两个文件,把test.h修改为:
#include <QtQuick/QQuickPaintedItem>
#include <QColor>
class test : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor)//定义qml文件里的color属性
public:
test(QQuickItem *parent = 0);
QColor color() const;
void setColor(const QColor &color);
void paint(QPainter *painter);
private:
QColor m_color;
};
test.cpp修改为:
#include "test.h"
#include <QPainter>
test::test(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
}
QColor test::color() const
{
return m_color;
}
void test::setColor(const QColor &color)
{
m_color = color;
}
void test::paint(QPainter *painter)
{
QPen pen(m_color, 2);
painter->setPen(pen);
painter->setRenderHints(QPainter::Antialiasing, true);
painter->drawPie(boundingRect().adjusted(1, 1, -1, -1), 90 * 16, 290 * 16);
}
Preview: 明天介绍多线程
页:
[1]