Qt5官方demo解析集28——Extending QML - Signal Support Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873
接上文Qt5官方demo解析集27——Extending
QML - Attached Properties Example
这个demo演示了为QML自己定义类型加入信号的方法。这与Qt5官方demo解析集16——Chapter
2: Connecting to C++ Methods and Signals所介绍的差点儿相同。鉴于样例的尺寸,可能那一篇要更清晰一些。
不管怎样,我们还是要继续扩展这个BirthdayParty例程。我们为这个生日派对加入了一个“派对開始”的信号,并定义了一个函数用来发射这个信号。
因为person类依旧没有改变。我们看看小小修改了的birthdayparty.h:
#ifndef BIRTHDAYPARTY_H
#define BIRTHDAYPARTY_H #include <QObject>
#include <QDate>
#include <qqml.h>
#include "person.h" class BirthdayPartyAttached : public QObject
{
Q_OBJECT
Q_PROPERTY(QDate rsvp READ rsvp WRITE setRsvp)
public:
BirthdayPartyAttached(QObject *object); QDate rsvp() const;
void setRsvp(const QDate &); private:
QDate m_rsvp;
}; class BirthdayParty : public QObject
{
Q_OBJECT
Q_PROPERTY(Person *host READ host WRITE setHost)
Q_PROPERTY(QQmlListProperty<Person> guests READ guests)
Q_CLASSINFO("DefaultProperty", "guests")
public:
BirthdayParty(QObject *parent = 0); Person *host() const;
void setHost(Person *); QQmlListProperty<Person> guests();
int guestCount() const;
Person *guest(int) const; static BirthdayPartyAttached *qmlAttachedProperties(QObject *); void startParty(); // 自己定义函数,之所以不使用Q_INVOKABLE是由于该函数是在main.cpp而不是QML中被调用的
// ![0]
signals:
void partyStarted(const QTime &time); // 自己定义信号
// ![0] private:
Person *m_host;
QList<Person *> m_guests;
};
QML_DECLARE_TYPEINFO(BirthdayParty, QML_HAS_ATTACHED_PROPERTIES) #endif // BIRTHDAYPARTY_H
birthdayParty.cpp:
#include "birthdayparty.h" BirthdayPartyAttached::BirthdayPartyAttached(QObject *object)
: QObject(object)
{
} QDate BirthdayPartyAttached::rsvp() const
{
return m_rsvp;
} void BirthdayPartyAttached::setRsvp(const QDate &d)
{
m_rsvp = d;
} BirthdayParty::BirthdayParty(QObject *parent)
: QObject(parent), m_host(0)
{
} Person *BirthdayParty::host() const
{
return m_host;
} void BirthdayParty::setHost(Person *c)
{
m_host = c;
} QQmlListProperty<Person> BirthdayParty::guests()
{
return QQmlListProperty<Person>(this, m_guests);
} int BirthdayParty::guestCount() const
{
return m_guests.count();
} Person *BirthdayParty::guest(int index) const
{
return m_guests.at(index);
} void BirthdayParty::startParty() // 该函数用来将当前时间作为信号參数发射出去
{
QTime time = QTime::currentTime();
emit partyStarted(time);
} BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object)
{
return new BirthdayPartyAttached(object);
}
example.qml:
import People 1.0
import QtQuick 2.0 // For QColor BirthdayParty {
// ![0]
onPartyStarted: console.log("This party started rockin' at " + time); // 在QML中我们不须要额外对信号进行处理
// ![0] // 仅仅须要关注与该信号相应的onXXX函数
// 当信号发出时该函数即被运行
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]
main.cpp:
#include <QCoreApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include "birthdayparty.h"
#include "person.h" int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv); qmlRegisterType<BirthdayPartyAttached>();
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
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()); // 上文讨论了qPrintable的问题
} party->startParty(); // 这里调用了发射信号函数,因此party開始时间将被打印在最后一行
} else {
qWarning() << component.errors();
} return 0;
}
结果如图:
Qt5官方demo解析集28——Extending QML - Signal Support Example的更多相关文章
- Qt5官方demo解析集30——Extending QML - Binding Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集29--Extendin ...
- Qt5官方demo解析集21——Extending QML - Adding Types Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 又是一个新的系列了,只是这个系列和我们之前的Chapt ...
- Qt5官方demo分析集29——Extending QML - Property Value Source Example
此系列的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集28--Extend ...
- 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 ...
- Qt5官方demo分析集11——Qt Quick Particles Examples - Affectors
在这个系列中的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集10--Qt ...
- Qt5官方demo分析集10——Qt Quick Particles Examples - Emitters
此系列的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 前段时间去听了Qt在北京的开发人员大会,感觉QML ...
- Dubbo系列(3)_官方Demo说明
一.本文目的 通过Dubbo的官方Demo介绍,学会搭建一个简单的Dubbo程序,包括服务端.客户端.接口等. Demo地址:https://github.com/alibaba/dubbo/ ...
随机推荐
- 微信、QQ中app的下载问题
最近在做一个项目,有一项功能是从微信中的分享页或者产品推广页面中下载app:在微信中直接下载app时微信是“拒绝”的,所以一般的做法是点击下载按钮弹出遮罩层,提示在浏览器中打开,然后进入外部浏览器,再 ...
- 【Django】ORM操作#2
目录 必知必会的13条查询方法 单表查询之神奇的双下划线 一对多 ForeignKey 多对多 ManyToManyField 在Python脚本中调用Django环境 Django终端打印SQL语句 ...
- USB摄像头驱动框架分析(五)
一.USB摄像头驱动框架如下所示:1.构造一个usb_driver2.设置 probe: 2.1. 分配video_device:video_device_alloc ...
- rpm2cpio---如何不安装但是获取rpm包中的文件
如何不安装但是获取rpm包中的文件 使用工具rpm2cpio和cpio rpm2cpio xxx.rpm | cpio -idmv 参数i表示提取文件.v表示指示执行进程,d和make-directo ...
- 树莓派3b+ wifi无线连接
一.配置文件启动wifi 配置 /etc/network/interfaces 文件实现,但在图形界面上并没有wifi图标可以选择,这种方法不够灵活,后面连接其它的wifi都要去修改配置文件 首先打开 ...
- Spring Cloud学习笔记【一】Eureka服务注册与发现
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...
- 洛谷 P1914 小书童——密码
P1914 小书童——密码 题目背景 某蒟蒻迷上了“小书童”,有一天登陆时忘记密码了(他没绑定邮箱or手机),于是便把问题抛给了神犇你. 题目描述 蒟蒻虽然忘记密码,但他还记得密码是由一串字母组成.且 ...
- mysql通过字段凝视查找字段名称
有时候表的字段太多.仅仅是大致记得表的凝视,想通过字段凝视查找字段名称,能够用例如以下语句: SELECT COLUMN_NAME,column_comment FROM INFORMATION_SC ...
- crontab FAQ
1.crontab变量问题 crontab中的脚本须要引入系统变量才干找到,否则crontab中的命令找不到系统变量,或者都写绝对路径. 2.1分钟运行一次 */1 * * * * /etc/keep ...
- 整理一些PHP开发安全问题
整理一些PHP开发安全问题 php给了开发人员极大的灵活性,可是这也为安全问题带来了潜在的隐患.最近须要总结一下以往的问题,在这里借翻译一篇文章同一时候加上自己开发的一些感触总结一下. 简单介绍 当开 ...