使用Qt Mobility的发布和订阅(Publish&Subscribe)
(→Publish & Subscribe) |
(→使用范例) |
||
| Line 22: | Line 22: | ||
</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中获取相关属性值。 | ||
Revision as of 06:16, 21 September 2010
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.1-tp/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中的值,通过这些值,可以订阅充电状态的改变通知。
repository target="RProperty"表示使用RProperty来获取通知并得到当前属性值。
/Qt_RProperty/chargingstatus同样为自定义Value Space Path。
注意: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测试通过。


