Qt5官方demo分析集29——Extending QML - Property Value Source Example
此系列的所有文章都可以在这里查看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的更多相关文章
- Qt5官方demo解析集30——Extending QML - Binding Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集29--Extendin ...
- Qt5官方demo解析集28——Extending QML - Signal Support Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集27--Extendin ...
- Qt5官方demo解析集21——Extending QML - Adding Types Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 又是一个新的系列了,只是这个系列和我们之前的Chapt ...
- Qt5官方demo分析集10——Qt Quick Particles Examples - Emitters
此系列的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 前段时间去听了Qt在北京的开发人员大会,感觉QML ...
- Qt5官方demo分析集11——Qt Quick Particles Examples - Affectors
在这个系列中的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集10--Qt ...
- Qt5官方demo解析集13——Qt Quick Particles Examples - Image Particles
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文 Qt5官方demo解析集12--Qt Quic ...
- Qt5官方demo解析集35——Music Player(使用winextras模块)
本系列所有文章可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集34——Concentr ...
- Qt5官方demo解析集(36个)
http://blog.csdn.net/cloud_castle/article/category/2123873 http://blog.csdn.net/cloud_castle/article ...
- RobotFramework RobotFramework官方demo Quick Start Guide浅析
RobotFramework官方demo Quick Start Guide浅析 by:授客 QQ:1033553122 博客:http://blog.sina.com.cn/ishouk ...
随机推荐
- 计时器 Timer
计时器 Timer 不多说了,守则.
- 查看oracle数据库的连接数以及用户
查看oracle数据库的连接数以及用户 11.查询oracle的连接数2select count(*) from v$session;32.查询oracle的并发连接数4select count(*) ...
- Hello ASP.NET5
2015年11月30日, ASP.NET 5 RC1 已经发布,本文尝试了一下ASP.NET5项目的创见一发布到IIS.开发环境,win10 64位,visual studio2015(已更新upda ...
- [生产环境数据恢复]innobackupex: fatal error: OR no 'datadir' option in group 'mysqld' in MySQL options
1 运行恢复命令 [xxx@xxx-c001db1 tmp]$ time /usr/bin/innobackupex --rsync --user="user" --passwo ...
- 百度云盘建svnserver步骤
安装tortoisesvn(略) 安装visualsvn(主server您可以使用)(略步骤)例如下面的安装文件夹后,: 注冊百度账号.进入百度云盘页面,建立目录,如mysvn. 下载百度云盘clie ...
- Android学习路径(十)怎么会Action Bar堆放在布局
默认情况下.action bar出如今activity窗体的顶部.稍微降低了activity布局的总空间. 假设你想隐藏或者显示action bar.在这堂用户体验的课程中,你能够通过调用hide() ...
- android 处理器crash刊物
日志记录程序是为了方便各种异常情况,为了方便日后的维修方案进行维修,程序无法百分百健康,完美,有必要保存在日志中代码.易于维护.Java了一个接口UncaughtExceptionHandler,Th ...
- java使用Base64编码和解码的图像文件
1.编码和解码下面的代码示例看: import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import j ...
- C++ Primer 学习笔记_35_STL实践与分析(9)--map种类(在)
STL实践与分析 --map类型(上) 引: map是键-值对的集合. map类型通常能够理解为关联数组:能够通过使用键作为下标来获取一个值,正如内置数组类型一样:而关联的本质在于元素的值与某个特定的 ...
- 【转】Android HTTP协议
前言 说到HTTP协议,那必须要说说WWW了,WWW是环球信息网(World Wide Web )的缩写,也可以简称为Web,中文名字为“万维网”.简单来说,WWW是以Internet作为传输媒介的一 ...