鱼C论坛

 找回密码
 立即注册
查看: 3036|回复: 0

[技术原创] QT动画制作(三十二)

[复制链接]
发表于 2017-3-22 06:05:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
效果:
jdfw.gif
还是新建一个quick application 项目,然后删去MainForm.ui.qml文件,把main.qml文件修改为:
  1. import QtQuick 2.5
  2. import Test 1.0
  3. Rectangle{
  4.     Test {
  5.         id: aPieChart
  6.         anchors.centerIn: parent
  7.         width: 100; height: 100
  8.         color: "green"
  9.     }
  10.     width:200
  11.     height: 200
  12.     color:"red"
  13.     RotationAnimator on rotation {
  14.         from:0
  15.         to:360
  16.         duration: 1000
  17.         loops:Animator.Infinite
  18.     }
  19. }
复制代码


main.cpp文件修改为:
  1. #include <QtGui/QGuiApplication>
  2. #include <QtQuick/QQuickView>
  3. #include <QtQml>
  4. #include "test.h"
  5. int main(int argc, char *argv[])
  6. {

  7.     QGuiApplication app(argc,argv);
  8.     qmlRegisterType<test>("Test", 1, 0, "Test");//注册test类为qml对象,第一个参数是对象名,第二和三个是版本号
  9.     QQuickView viewer;
  10.     viewer.setResizeMode(QQuickView::SizeRootObjectToView);
  11.     viewer.setSource(QUrl("qrc:/main.qml"));
  12.     viewer.show();

  13.     return app.exec();
  14. }

复制代码

然后在总目录选添加新文件,添加C++ class文件名为test的文件,系统会自动生成test.h和test.cpp两个文件,把test.h修改为:
  1. #include <QtQuick/QQuickPaintedItem>
  2. #include <QColor>

  3. class test : public QQuickPaintedItem
  4. {
  5.     Q_OBJECT
  6.     Q_PROPERTY(QColor color READ color WRITE setColor)//定义qml文件里的color属性

  7. public:
  8.     test(QQuickItem *parent = 0);
  9.     QColor color() const;
  10.     void setColor(const QColor &color);
  11.     void paint(QPainter *painter);

  12. private:
  13.     QColor m_color;
  14. };
复制代码

test.cpp修改为:
  1. #include "test.h"
  2. #include <QPainter>


  3. test::test(QQuickItem *parent)
  4.     : QQuickPaintedItem(parent)
  5. {
  6. }

  7. QColor test::color() const
  8. {
  9.     return m_color;
  10. }

  11. void test::setColor(const QColor &color)
  12. {
  13.     m_color = color;
  14. }


  15. void test::paint(QPainter *painter)
  16. {
  17.     QPen pen(m_color, 2);
  18.     painter->setPen(pen);
  19.     painter->setRenderHints(QPainter::Antialiasing, true);
  20.     painter->drawPie(boundingRect().adjusted(1, 1, -1, -1), 90 * 16, 290 * 16);
  21. }


复制代码

Preview: 明天介绍多线程

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-4 03:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表