使用Qt Mobility的发布和订阅(Publish&Subscribe)
(Created page with '==<u>'''Qt Mobility介绍'''</u>== Qt Mobility是一个开源项目,它封装了手机特性相关的功能,请参考[[在Qt_S60中使用QtMobility做开发 | 在Qt_S60中使…') |
hamishwillee
(Talk | contribs) m (Text replace - "<code cpp>" to "<code cpp-qt>") |
||
| (18 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
| + | [[Category:Lang-Chinese]][[Category:Qt Mobility]] | ||
| + | {{ArticleMetaData | ||
| + | |sourcecode=[[Media:Qt_PublishSubscribe.zip]] | ||
| + | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
| + | |devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') --> | ||
| + | |sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) --> | ||
| + | |platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later --> | ||
| + | |devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) --> | ||
| + | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
| + | |signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | ||
| + | |keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase --> | ||
| + | |id= <!-- Article Id (Knowledge base articles only) --> | ||
| + | |language=Lang-Chinese | ||
| + | |translated-by= <!-- [[User:XXXX]] --> | ||
| + | |translated-from-title= <!-- Title only --> | ||
| + | |translated-from-id= <!-- Id of translated revision --> | ||
| + | |review-by=<!-- After re-review: [[User:username]] --> | ||
| + | |review-timestamp= <!-- After re-review: YYYYMMDD --> | ||
| + | |update-by= <!-- After significant update: [[User:username]]--> | ||
| + | |update-timestamp= <!-- After significant update: YYYYMMDD --> | ||
| + | |creationdate=20100920 | ||
| + | |author=[[User:Cxt programmer]] | ||
| + | }} | ||
| + | |||
==<u>'''Qt Mobility介绍'''</u>== | ==<u>'''Qt Mobility介绍'''</u>== | ||
| − | Qt | + | Qt Mobility是Nokia的一个开源项目,它封装了手机特性相关的功能,请参考[[在Qt_S60中使用QtMobility做开发 | 在Qt S60中使用QtMobility做开发]]。 |
| − | ==<u>'''Publish&Subscribe'''</u>== | + | ==<u>'''Publish & Subscribe'''</u>== |
在Symbian平台,Publish & Subscribe是从实时内核(EKA2)后开始提供的新API。 | 在Symbian平台,Publish & Subscribe是从实时内核(EKA2)后开始提供的新API。 | ||
| Line 9: | Line 34: | ||
'''Qt Mobility_Publish & Subscribe(For Symbian) API'''也提供了同样的功能,它们封装了Symbian平台的RProperty和CRepository,以提供和Symbian Publish & Subscribe一样的功能。 | '''Qt Mobility_Publish & Subscribe(For Symbian) API'''也提供了同样的功能,它们封装了Symbian平台的RProperty和CRepository,以提供和Symbian Publish & Subscribe一样的功能。 | ||
| − | Qt Publish & Subscribe详细描述可参考文档:http://doc.qt.nokia.com/qtmobility-1. | + | Qt Publish & Subscribe详细描述可参考文档:http://doc.qt.nokia.com/qtmobility-1.0/publ-subs.html |
==<u>'''使用范例'''</u>== | ==<u>'''使用范例'''</u>== | ||
下面我们展示一下如何使用Qt Publish & Subscribe API获取系统情景模式改变、以及充电状态通知。 | 下面我们展示一下如何使用Qt Publish & Subscribe API获取系统情景模式改变、以及充电状态通知。 | ||
| − | 使用Qt Publish & Subscribe | + | 使用Qt Publish & Subscribe API,需要自己定义QCRML文件,该文件描述了应用程序在Publish或Subscribe时所关心的“'''Key UIDs'''”。如果想获取情景模式的改变通知,QCRML文件定义如下(<span style="color:#ff0000"> '''为避免文件名重复,最好使用应用程序UID来命名'''</span>): |
| − | <code cpp> | + | <code cpp-qt> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
<repository target="CRepository" uidValue="0x101F8798"> | <repository target="CRepository" uidValue="0x101F8798"> | ||
| Line 22: | Line 47: | ||
</code> | </code> | ||
| − | 熟悉Symbian的朋友可以看到,0x101F8798和0x7E000001是定义在S60 | + | 熟悉Symbian的朋友可以看到,0x101F8798和0x7E000001是定义在S60 SDK头文件'''profileenginesdkcrkeys.h''' (可以在S60的安装SDK中找到)中的值,通过这些值,可以订阅情景模式的改变通知。 |
'''repository target="CRepository"'''表示使用CRepository从系统Central Repository中获取相关属性值。 | '''repository target="CRepository"'''表示使用CRepository从系统Central Repository中获取相关属性值。 | ||
| − | '''/Qt_P_S/activeProfile'''为自定义Value Space路径,Qt Value Space使用分层树状结构定义路径,详情请参考:http://doc.qt.nokia.com/qtmobility-1. | + | '''/Qt_P_S/activeProfile'''为自定义Value Space路径,Qt Value Space使用分层树状结构定义路径,详情请参考:http://doc.qt.nokia.com/qtmobility-1.0/publ-subs.html#declaring-value-space-paths |
为了获取充电状态通知,还需定义QCRML文件如下: | 为了获取充电状态通知,还需定义QCRML文件如下: | ||
| − | <code cpp> | + | <code cpp-qt> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
<repository target="RProperty" uidValue="0x10205041"> | <repository target="RProperty" uidValue="0x10205041"> | ||
| Line 36: | Line 61: | ||
</code> | </code> | ||
| − | 熟悉Symbian的朋友同样可以看到0x10205041和0x00000003是定义在S60 | + | 熟悉Symbian的朋友同样可以看到0x10205041和0x00000003是定义在S60 SDK头文件'''HWRMPowerStateSDKPSKeys.h'''(可以在S60的安装SDK中找到)中的值,通过这些值,可以订阅充电状态的改变通知。 |
'''repository target="RProperty"'''表示使用RProperty来获取通知并得到当前属性值。 | '''repository target="RProperty"'''表示使用RProperty来获取通知并得到当前属性值。 | ||
'''/Qt_RProperty/chargingstatus'''同样为自定义Value Space Path。 | '''/Qt_RProperty/chargingstatus'''同样为自定义Value Space Path。 | ||
| + | |||
| + | 如果定义同一个“'''Key UIDs'''”下的不同属性,只需增加多条“'''key ref'''”即可,例如增加一个“电池电量”属性: | ||
| + | <code cpp-qt> | ||
| + | <?xml version="1.0" encoding="UTF-8"?> | ||
| + | <repository target="RProperty" uidValue="0x10205041"> | ||
| + | <key ref="/Qt_RProperty/chargingstatus" int="0x00000003"/> | ||
| + | <key ref="/Qt_RProperty/batteryLevel" int="0x00000001"/> | ||
| + | </repository> | ||
| + | </code> | ||
| + | |||
| + | 像'''<key ref="/Qt_RProperty/batteryLevel" int="0x00000001"/>'''一样增加其他属性就可以了。 | ||
'''<span style="color:#ff0000">注意:</span>'''QCRML文件必须安装到手机有效盘符的\resource\qt\crml\目录下,否则API将不起作用。 | '''<span style="color:#ff0000">注意:</span>'''QCRML文件必须安装到手机有效盘符的\resource\qt\crml\目录下,否则API将不起作用。 | ||
| Line 46: | Line 82: | ||
==<u>'''相关源文件'''</u>== | ==<u>'''相关源文件'''</u>== | ||
MySubscribe.h | MySubscribe.h | ||
| − | <code cpp> | + | <code cpp-qt> |
/* | /* | ||
| Line 96: | Line 132: | ||
</code> | </code> | ||
| − | '''当使用Mobility | + | '''当使用Mobility API时,需要使用 <font color ="red">QTM_USE_NAMESPACE</font>;当前置声明时,需要用 <font color ="red">QTM_BEGIN_NAMESPACE</font> 和 <font color="red">QTM_END_NAMESPACE</font> 将前置声明“包围”,否则会出编译错误。''' |
MySubscribe.cpp | MySubscribe.cpp | ||
| − | <code cpp> | + | <code cpp-qt> |
/* | /* | ||
| Line 217: | Line 253: | ||
[[File:Qt_PublishSubscribe_Screenshot.jpg]] | [[File:Qt_PublishSubscribe_Screenshot.jpg]] | ||
| + | |||
| + | ==<u>'''测试设备'''</u>== | ||
| + | N97mini、N8测试通过。 | ||
==<u>'''相关链接'''</u>== | ==<u>'''相关链接'''</u>== | ||
| − | *[[Qt_开发|Qt 开发]] | + | *[[Qt_开发|Qt 开发]][[Category:MeeGo Harmattan]] [[Category:Symbian]] |
| − | + | ||
| − | + | ||
| − | [[Category: | + | |
Latest revision as of 04:23, 11 October 2012
文章信息
Contents |
Qt Mobility介绍
Qt Mobility是Nokia的一个开源项目,它封装了手机特性相关的功能,请参考 在Qt S60中使用QtMobility做开发。
Publish & Subscribe
在Symbian平台,Publish & Subscribe是从实时内核(EKA2)后开始提供的新API。
Publish & Subscribe是进程间通信的另一种方式,它允许“发布者进程”定义和更新一系列属性;其他进程,称为“订阅者进程”,可以监听属性的改变,并得到属性值。
Qt Mobility_Publish & Subscribe(For Symbian) API也提供了同样的功能,它们封装了Symbian平台的RProperty和CRepository,以提供和Symbian Publish & Subscribe一样的功能。
Qt Publish & Subscribe详细描述可参考文档:http://doc.qt.nokia.com/qtmobility-1.0/publ-subs.html
使用范例
下面我们展示一下如何使用Qt Publish & Subscribe API获取系统情景模式改变、以及充电状态通知。
使用Qt Publish & Subscribe API,需要自己定义QCRML文件,该文件描述了应用程序在Publish或Subscribe时所关心的“Key UIDs”。如果想获取情景模式的改变通知,QCRML文件定义如下( 为避免文件名重复,最好使用应用程序UID来命名):
<?xml version="1.0" encoding="UTF-8"?>
<repository target="CRepository" uidValue="0x101F8798">
<key ref="/Qt_P_S/activeProfile" int="0x7E000001"/>
</repository>
熟悉Symbian的朋友可以看到,0x101F8798和0x7E000001是定义在S60 SDK头文件profileenginesdkcrkeys.h (可以在S60的安装SDK中找到)中的值,通过这些值,可以订阅情景模式的改变通知。
repository target="CRepository"表示使用CRepository从系统Central Repository中获取相关属性值。
/Qt_P_S/activeProfile为自定义Value Space路径,Qt Value Space使用分层树状结构定义路径,详情请参考:http://doc.qt.nokia.com/qtmobility-1.0/publ-subs.html#declaring-value-space-paths
为了获取充电状态通知,还需定义QCRML文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<repository target="RProperty" uidValue="0x10205041">
<key ref="/Qt_RProperty/chargingstatus" int="0x00000003"/>
</repository>
熟悉Symbian的朋友同样可以看到0x10205041和0x00000003是定义在S60 SDK头文件HWRMPowerStateSDKPSKeys.h(可以在S60的安装SDK中找到)中的值,通过这些值,可以订阅充电状态的改变通知。
repository target="RProperty"表示使用RProperty来获取通知并得到当前属性值。
/Qt_RProperty/chargingstatus同样为自定义Value Space Path。
如果定义同一个“Key UIDs”下的不同属性,只需增加多条“key ref”即可,例如增加一个“电池电量”属性:
<?xml version="1.0" encoding="UTF-8"?>
<repository target="RProperty" uidValue="0x10205041">
<key ref="/Qt_RProperty/chargingstatus" int="0x00000003"/>
<key ref="/Qt_RProperty/batteryLevel" int="0x00000001"/>
</repository>
像<key ref="/Qt_RProperty/batteryLevel" int="0x00000001"/>一样增加其他属性就可以了。
注意:QCRML文件必须安装到手机有效盘符的\resource\qt\crml\目录下,否则API将不起作用。
相关源文件
MySubscribe.h
/*
* MySubscribe.h
*
* Created on: 2010-9-8
* Author: Cxt_programmer
*/
#ifndef __MY_SUBSCRIBE_H__
#define __MY_SUBSCRIBE_H__
#include <qmobilityglobal.h>
#include <QObject>
QTM_BEGIN_NAMESPACE
class QValueSpaceSubscriber;
QTM_END_NAMESPACE
QTM_USE_NAMESPACE
class MySubscribe : public QObject
{
Q_OBJECT
public:
MySubscribe( QObject* parent = 0 );
virtual ~MySubscribe();
signals:
void UpdateProfileLabel( const QString& curProfile );
void UpdateChargingStatusLabel( const int curChargingStatus );
private slots:
void subscribeChanged();
void chargingSubscribeChanged();
public:
void ChangeToProfile( int profileId );
void Initialize();
private:
QValueSpaceSubscriber* profileSubScribe;
QValueSpaceSubscriber* chargingSubscribe;
};
#endif /* __MY_SUBSCRIBE_H__ */
当使用Mobility API时,需要使用 QTM_USE_NAMESPACE;当前置声明时,需要用 QTM_BEGIN_NAMESPACE 和 QTM_END_NAMESPACE 将前置声明“包围”,否则会出编译错误。
MySubscribe.cpp
/*
* MySubscribe.cpp
*
* Created on: 2010-9-8
* Author: Cxt_programmer
*/
#include "MySubscribe.h"
#include "ProfileIdDefine.h"
#include <qvaluespacesubscriber.h>
#include <QValueSpace.h>
#include <QtDebug>
#include <QValueSpacePublisher>
// Review E2BEBC78.qcrml file for more details
#define PATH "/Qt_P_S/activeProfile"
#define ROOTPATH "/Qt_P_S"
#define SUBPATH "activeProfile"
#define CHARGING_PATH "/Qt_RProperty/chargingstatus"
// end of Review
// Macro Define
#define CHANGED_TO_GENERAL "Current profile is: General"
#define CHANGED_TO_SILENT "Current profile is: Silent"
#define CHANGED_TO_Meeting "Current profile is: Meeting"
#define CHANGED_TO_Outdoor "Current profile is: Outdoor"
#define CHANGED_TO_Pager "Current profile is: Pager"
#define CHANGED_TO_OFFLINE "Current profile is: Offline"
// end of Macro
MySubscribe::MySubscribe( QObject* parent )
{
// subScribe = new QValueSpaceSubscriber( QVALUESPACE_SYMBIAN_SETTINGS_LAYER, "/Qt_P_S/activeProfile" );
profileSubScribe = new QValueSpaceSubscriber( PATH );
chargingSubscribe = new QValueSpaceSubscriber( CHARGING_PATH );
connect( profileSubScribe, SIGNAL(contentsChanged()), this, SLOT(subscribeChanged()) );
connect( chargingSubscribe, SIGNAL(contentsChanged()), this, SLOT(chargingSubscribeChanged()) );
}
MySubscribe::~MySubscribe()
{
delete profileSubScribe;
delete chargingSubscribe;
}
void MySubscribe::Initialize()
{
subscribeChanged();
chargingSubscribeChanged();
}
void MySubscribe::subscribeChanged()
{
QVariant variant = profileSubScribe->value();
int currentProfileId = variant.toInt();
switch ( currentProfileId )
{
case PROFILE_GENERAL:
emit UpdateProfileLabel( CHANGED_TO_GENERAL );
break;
case PROFILE_SILENT:
emit UpdateProfileLabel( CHANGED_TO_SILENT );
break;
case PROFILE_MEETING:
emit UpdateProfileLabel( CHANGED_TO_Meeting );
break;
case PROFILE_OUTDOOR:
emit UpdateProfileLabel( CHANGED_TO_Outdoor );
break;
case PROFILE_PAGER:
emit UpdateProfileLabel( CHANGED_TO_Pager );
break;
case PROFILE_OFFLINE:
emit UpdateProfileLabel( CHANGED_TO_OFFLINE );
break;
default:
break;
}
}
void MySubscribe::chargingSubscribeChanged()
{
QVariant chargingStatus = chargingSubscribe->value();
emit UpdateChargingStatusLabel( chargingStatus.toInt() );
}
void MySubscribe::ChangeToProfile( int profileId )
{
// QValueSpacePublisher publisher( ROOTPATH );
// publisher.setValue( SUBPATH, profileId );
// publisher.sync();
}
在该代码中,由于订阅了两个不同的属性通知——情景模式和充电状态,所以工程中写了2个QCRML文件,我曾尝试将2个属性定义在一个QCRML文件中,没有成功,如果有朋友有更好的方法处理,欢迎修改此篇文章,与大家分享。
测试工程
完整测试工程下载:File:Qt PublishSubscribe.zip。
工程说明
执行程序后,主界面会看到两个Label,分别显示了当前的情景模式和充电状态,当情景模式或充电状态发生改变时,Label文字也会相应的改变。
测试设备
N97mini、N8测试通过。


