此系列的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873

接上文Qt5官方demo解析集28——Extending
QML - Signal Support Example

我们常常会在QML代码中使用Animation和bindings。以使得我们的程序具有更好的动态性能。那么,类似NumberAnimation这样的QML类似实际上是提供了一个算法来为属性提供动态变化的数值。或者说是提供了一个值的集合。这里Qt将其称作“属性值来源”(Property Value Source),并为这些QML类型提供了一个通用的接口。即QQmlPropertyValueSource。通过继承这个类,我们能够实现自己定义的Property
Value Source。

在前面的项目中加入一个类happybirthdaysong,用来自己定义地控制BirthdayParty中announcement属性的变化:

这个demo向我们展示了这个自己定义的过程。happybirthdaysong.h:

#ifndef HAPPYBIRTHDAYSONG_H
#define HAPPYBIRTHDAYSONG_H #include <QQmlPropertyValueSource>
#include <QQmlProperty>
#include <qqml.h> #include <QStringList> // ![0]
class HappyBirthdaySong : public QObject, public QQmlPropertyValueSource // 因为QQmlPropertyValueSource是一个接口类
{ // 我们还须要继承QObject
Q_OBJECT
Q_INTERFACES(QQmlPropertyValueSource) // 声明接口
// ![0]
Q_PROPERTY(QString name READ name WRITE setName) // name属性用来设置生日歌的对象
// ![1]
public:
HappyBirthdaySong(QObject *parent = 0); virtual void setTarget(const QQmlProperty &); // 用来指明作用的属性对象
// ![1] // <PropertyValueSource> on <property>时被调用 QString name() const; // 自己定义属性的读写函数
void setName(const QString &); private slots:
void advance(); // 更新函数,每秒输出一句歌词 private:
int m_line;
QStringList m_lyrics;
QQmlProperty m_target;
QString m_name;
// ![2]
};
// ![2] #endif // HAPPYBIRTHDAYSONG_H

happybirthdaysong.cpp:

#include "happybirthdaysong.h"
#include <QTimer> HappyBirthdaySong::HappyBirthdaySong(QObject *parent)
: QObject(parent), m_line(-1) // 初始化m_line为-1
{ // 使advance()第一次被调用时输出第一句歌词
setName(QString());
QTimer *timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance()));
timer->start(1000);
} void HappyBirthdaySong::setTarget(const QQmlProperty &p)
{
m_target = p; // 该类型作用于某个属性时,Qt会使用这里的函数
} QString HappyBirthdaySong::name() const
{
return m_name;
} void HappyBirthdaySong::setName(const QString &name) // 初始化歌词。并带上“姓名”參数
{
m_name = name; m_lyrics.clear();
m_lyrics << "Happy birthday to you,";
m_lyrics << "Happy birthday to you,";
m_lyrics << "Happy birthday dear " + m_name + ",";
m_lyrics << "Happy birthday to you!";
m_lyrics << "";
} void HappyBirthdaySong::advance() // 循环显示的好方式
{
m_line = (m_line + 1) % m_lyrics.count(); m_target.write(m_lyrics.at(m_line));
}

Person类没有变化,而BirthdayParty类则单纯地加入了一个属性announcement来使上面的Source能作用其上,它与其它属性没有不同,类型为QString。用来赋予不同的歌词。

Q_PROPERTY(QString announcement READ announcement WRITE setAnnouncement)

main.cpp也没有修改:

#include <QCoreApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include "birthdayparty.h"
#include "happybirthdaysong.h"
#include "person.h" int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv); qmlRegisterType<BirthdayPartyAttached>();
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
qmlRegisterType<HappyBirthdaySong>("People", 1,0, "HappyBirthdaySong");
qmlRegisterType<ShoeDescription>();
qmlRegisterType<Person>();
qmlRegisterType<Boy>("People", 1,0, "Boy");
qmlRegisterType<Girl>("People", 1,0, "Girl"); QQmlEngine engine;
QQmlComponent component(&engine, QUrl("qrc:example.qml"));
BirthdayParty *party = qobject_cast<BirthdayParty *>(component.create()); if (party && party->host()) {
qWarning() << party->host()->name() << "is having a birthday!"; if (qobject_cast<Boy *>(party->host()))
qWarning() << "He is inviting:";
else
qWarning() << "She is inviting:"; for (int ii = 0; ii < party->guestCount(); ++ii) {
Person *guest = party->guest(ii); QDate rsvpDate;
QObject *attached =
qmlAttachedPropertiesObject<BirthdayParty>(guest, false);
if (attached)
rsvpDate = attached->property("rsvp").toDate(); if (rsvpDate.isNull())
qWarning() << " " << guest->name() << "RSVP date: Hasn't RSVP'd";
else
qWarning() << " " << guest->name() << "RSVP date:" << qPrintable(rsvpDate.toString());
} party->startParty();
} else {
qWarning() << component.errors();
} return app.exec();
}

最后。这个Source须要通过<PropertyValueSource> on <property>这种语句来调用,

因此在QML文件里加入了HappyBirthdaySongonannouncement{name:"BobJones"},使得这个程序得以循环地为Bob
Jones唱生日快乐歌:

example.qml:

import People 1.0
import QtQuick 2.0 // For QColor // ![0]
BirthdayParty {
HappyBirthdaySong on announcement { name: "Bob Jones" }
// ![0] onPartyStarted: console.log("This party started rockin' at " + time); host: Boy {
name: "Bob Jones"
shoe { size: 12; color: "white"; brand: "Nike"; price: 90.0 }
} Boy {
name: "Leo Hodges"
BirthdayParty.rsvp: "2009-07-06"
shoe { size: 10; color: "black"; brand: "Reebok"; price: 59.95 }
}
Boy {
name: "Jack Smith"
shoe { size: 8; color: "blue"; brand: "Puma"; price: 19.95 }
}
Girl {
name: "Anne Brown"
BirthdayParty.rsvp: "2009-07-01"
shoe.size: 7
shoe.color: "red"
shoe.brand: "Marc Jacobs"
shoe.price: 699.99
} // ![1]
}
// ![1]

效果例如以下:

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Qt5官方demo分析集29——Extending QML - Property Value Source Example的更多相关文章

  1. Qt5官方demo解析集30——Extending QML - Binding Example

    本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集29--Extendin ...

  2. Qt5官方demo解析集28——Extending QML - Signal Support Example

    本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集27--Extendin ...

  3. Qt5官方demo解析集21——Extending QML - Adding Types Example

    本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 又是一个新的系列了,只是这个系列和我们之前的Chapt ...

  4. Qt5官方demo分析集10——Qt Quick Particles Examples - Emitters

    此系列的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 前段时间去听了Qt在北京的开发人员大会,感觉QML ...

  5. Qt5官方demo分析集11——Qt Quick Particles Examples - Affectors

    在这个系列中的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集10--Qt ...

  6. Qt5官方demo解析集13——Qt Quick Particles Examples - Image Particles

    本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文 Qt5官方demo解析集12--Qt Quic ...

  7. Qt5官方demo解析集35——Music Player(使用winextras模块)

    本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集34——Concentr ...

  8. Qt5官方demo解析集(36个)

    http://blog.csdn.net/cloud_castle/article/category/2123873 http://blog.csdn.net/cloud_castle/article ...

  9. RobotFramework RobotFramework官方demo Quick Start Guide浅析

    RobotFramework官方demo Quick Start Guide浅析   by:授客 QQ:1033553122     博客:http://blog.sina.com.cn/ishouk ...

随机推荐

  1. 妈蛋:kinMaxShow旋转木马异常,WebUploader图片上传坑爹,图像被压缩

    今天晚上在改造轮播图. 原来的代码是这种: <div> <img src="${static}/image/index/banner/`.jpg" /> & ...

  2. 发现SQL Server惊天大秘密!!

    原文:发现SQL Server惊天大秘密!! --set statistics xml onCREATE TABLE T_TEST(ID INT IDENTITY PRIMARY KEY,Create ...

  3. 怎么将Emeditor设置成网页查看源代码的默认编译器

    1.打开emditor: 2.在菜单栏中找到工具---->自定义,打开自定义窗口: 3.快捷方式--->更多快捷方式 5.选中“在internet explorer中通过emeditor查 ...

  4. Html5用Canvas制作画图板

    需求: 绘制多边形 可填充颜色 可设置文字 可移动,可删除 鼠标按住后,抬起之前线段应该尾随鼠标当前位置 可与后台方便的进行数据交互,保存到后台,或将数据从后台取到前台显示对应的图形 思考: 第一想到 ...

  5. Chapter 2 User Authentication, Authorization, and Security(4):限制SA帐户管理权限

    原版的:http://blog.csdn.net/dba_huangzj/article/details/38817915,专题文件夹:http://blog.csdn.net/dba_huangzj ...

  6. RH133读书笔记(8)-Lab 8 Manage Network Settings

    Lab 8 Manage Network Settings Goal: To build skills needed to manually configure networking Estimate ...

  7. RH133读书笔记(11)-Lab 11 System Rescue and Troubleshooting

    Lab 11 System Rescue and Troubleshooting Goal: To build skills in system rescue procedures. Estimate ...

  8. 【翻译】Ext JS最新技巧——2014-10-30

    原文:Top Support Tips Greg Barry:Ext JS 5的ExtraParams Ext JS 4同意用户直接将extraParams加入到一个链接,相似例如以下代码: Ext. ...

  9. swift 它们的定义TabBarItem

    1.效果图     2.NewsViewController.swift // // NewsViewController.swift // NavigationDemo // // Created ...

  10. Androida规划nt打包

    1.准备工作 (1)首先安装好ant工具 (2)生成keystore 在jdk的bin文件夹下 输入 keytool -genkey -alias android.keystore -keyalg R ...