使用Qt Mobility Messaging API发送短信
hamishwillee
(Talk | contribs) m (Text replace - "<code cpp>" to "<code cpp-qt>") |
|||
| (21 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | + | [[Category:Code Examples]][[Category:S60]][[Category:Lang-Chinese]][[Category:Qt Mobility]][[Category:Messaging]] | |
| − | {{ | + | {{ArticleMetaData |
| − | | | + | |language=Lang-Chinese |
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition Symbian^3 | |platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition Symbian^3 | ||
|devices= Nokia 5800 and N8. | |devices= Nokia 5800 and N8. | ||
| − | + | |creationdate=20101118 | |
| − | + | ||
| − | |creationdate= | + | |
|keywords= QMessageService, QMessage, Chinese | |keywords= QMessageService, QMessage, Chinese | ||
| + | |sourcecode=[[Media:SendSMS.zip]] | ||
| + | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
| + | |sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) --> | ||
| + | |devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) --> | ||
| + | |signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities=NetworkServices, LocalServices, ReadUserData, WriteUserData, UserEnvironment, ReadDeviceData, WriteDeviceData | ||
| + | |author=[[User:Flycarl]] | ||
}} | }} | ||
| Line 15: | Line 20: | ||
这里的代码演示如何用Qt Mobility的Messaging API发送短信,自Qt Mobility 1.1.0可以发送中文短信。 | 这里的代码演示如何用Qt Mobility的Messaging API发送短信,自Qt Mobility 1.1.0可以发送中文短信。 | ||
代码需要以下能力 [[NetworkServices]] [[LocalServices]] [[ReadUserData]] [[WriteUserData]] [[UserEnvironment]] [[ReadDeviceData]] [[WriteDeviceData]] 。自签名不够用,至少需要 [[Developer certificate | 开发者签名]] ,也可以用 [[Open Signed Online]]来测试。 | 代码需要以下能力 [[NetworkServices]] [[LocalServices]] [[ReadUserData]] [[WriteUserData]] [[UserEnvironment]] [[ReadDeviceData]] [[WriteDeviceData]] 。自签名不够用,至少需要 [[Developer certificate | 开发者签名]] ,也可以用 [[Open Signed Online]]来测试。 | ||
| − | |||
==准备条件== | ==准备条件== | ||
| Line 25: | Line 29: | ||
在pro文件中增加这些行 | 在pro文件中增加这些行 | ||
| − | <code cpp> | + | <code cpp-qt> |
CONFIG += mobility | CONFIG += mobility | ||
MOBILITY = messaging | MOBILITY = messaging | ||
| Line 42: | Line 46: | ||
==代码== | ==代码== | ||
| − | + | 下面的代码片段演示了如何发短信,当使用Mobility API 头文件时,需要使用<font color ="red">QTM_USE_NAMESPACE</font>,否则会找不到头文件;当前置声明时,需要用 <font color ="red">QTM_BEGIN_NAMESPACE</font> 和 <font color="red">QTM_END_NAMESPACE</font> 将前置声明“包围”,否则会出编译错误。 | |
| − | + | <code cpp-qt> | |
| − | + | ||
| − | <code cpp> | + | |
#include <QMessageService> | #include <QMessageService> | ||
#include <QMessage> | #include <QMessage> | ||
| Line 52: | Line 54: | ||
QTM_USE_NAMESPACE | QTM_USE_NAMESPACE | ||
</code> | </code> | ||
| + | 这里的代码片段使用了一个 有预设字符的lineEdit来存放要发送的字符,可以是中文字符,这样做避免了关于字符编码的冗长讨论。实际操作中也不建议将中文字符硬编码在代码里,需要使用中文字符的地方应遵循[[Qt的国际化和本地化]]的操作。 | ||
| + | <code cpp-qt> | ||
| + | #include "mainwindow.h" | ||
| + | #include "ui_mainwindow.h" | ||
| + | #include <QMessage> | ||
| + | // Use Qt Mobility API namespace | ||
| + | QTM_USE_NAMESPACE | ||
| − | |||
MainWindow::MainWindow(QWidget *parent) : | MainWindow::MainWindow(QWidget *parent) : | ||
QMainWindow(parent), | QMainWindow(parent), | ||
| Line 59: | Line 67: | ||
{ | { | ||
ui->setupUi(this); | ui->setupUi(this); | ||
| + | service = new QMessageService(this); | ||
| + | connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(sendMessage())); | ||
| + | } | ||
| − | + | void MainWindow::sendMessage() | |
| + | { | ||
QMessage message; | QMessage message; | ||
QMessageAddressList toList; | QMessageAddressList toList; | ||
QMessageAddress::Type addrType(QMessageAddress::Phone); | QMessageAddress::Type addrType(QMessageAddress::Phone); | ||
| − | toList.append(QMessageAddress(addrType, | + | toList.append(QMessageAddress(addrType, ui->numberEdit->text()));//电话号码//这里可以设置群以号码 |
message.setType(QMessage::Sms);//短信类型 | message.setType(QMessage::Sms);//短信类型 | ||
message.setTo(toList); | message.setTo(toList); | ||
| − | message.setBody(ui->messageEdit->text()); | + | message.setBody(ui->messageEdit->text()); //设置短信内容为lineEdit中的字符,支持中文 |
| − | service | + | service->send(message); |
| + | } | ||
| + | |||
| + | MainWindow::~MainWindow() | ||
| + | { | ||
| + | delete ui; | ||
} | } | ||
</code> | </code> | ||
| + | <br> | ||
| + | QMessageService 调用symbian的API发送短信,这里的构造函数使用AO异步实现,不能把这里的service做成局部变量,否则有可能应为构造没完成,导致调用send方法时出错,报“A data abort exception has occurred” | ||
| + | <code cpp-qt> | ||
| + | #ifndef MAINWINDOW_H | ||
| + | #define MAINWINDOW_H | ||
| − | + | #include <QMainWindow> | |
| + | #include <QMessageService> | ||
| + | QTM_USE_NAMESPACE | ||
| − | + | namespace Ui { | |
| + | class MainWindow; | ||
| + | } | ||
| + | |||
| + | class MainWindow : public QMainWindow | ||
| + | { | ||
| + | Q_OBJECT | ||
| + | |||
| + | public: | ||
| + | explicit MainWindow(QWidget *parent = 0); | ||
| + | ~MainWindow(); | ||
| + | public slots: | ||
| + | void sendMessage(); | ||
| + | |||
| + | private: | ||
| + | Ui::MainWindow *ui; | ||
| + | QMessageService *service; | ||
| + | }; | ||
| + | |||
| + | #endif // MAINWINDOW_H | ||
| + | </code> | ||
| + | ==下载Demo== | ||
| + | [[File:SendSMS.zip]][[Category:MeeGo Harmattan]] [[Category:Symbian]] | ||
Revision as of 04:23, 11 October 2012
文章信息
代码示例
源文件: Media:SendSMS.zip
测试基于
设备:: Nokia 5800 and N8.
兼容于
平台: S60 3rd Edition, FP1, FP2
S60 5th Edition Symbian^3
S60 5th Edition Symbian^3
平台安全性
能力: NetworkServices, LocalServices, ReadUserData, WriteUserData, UserEnvironment, ReadDeviceData, WriteDeviceData
文章
关键词: QMessageService, QMessage, Chinese
由 flycarl
在 18 Nov 2010 创建
最后由 hamishwillee
在 11 Oct 2012 编辑
Contents |
简介
这里的代码演示如何用Qt Mobility的Messaging API发送短信,自Qt Mobility 1.1.0可以发送中文短信。 代码需要以下能力 NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData 。自签名不够用,至少需要 开发者签名 ,也可以用 Open Signed Online来测试。
准备条件
- 安装Qt for Symbian:Qt Symbian 开发环境安装
- 安装QtMobility API 1.1.0 及以上版本支持中文短信 :Qt Mobility 在Symbian平台的安装
编辑pro文件
在pro文件中增加这些行
CONFIG += mobility
MOBILITY = messaging
symbian {
TARGET.CAPABILITY = NetworkServices \
LocalServices \
ReadUserData \
WriteUserData \
UserEnvironment \
ReadDeviceData \
WriteDeviceData
}
代码
下面的代码片段演示了如何发短信,当使用Mobility API 头文件时,需要使用QTM_USE_NAMESPACE,否则会找不到头文件;当前置声明时,需要用 QTM_BEGIN_NAMESPACE 和 QTM_END_NAMESPACE 将前置声明“包围”,否则会出编译错误。
#include <QMessageService>
#include <QMessage>
// Use Qt Mobility API namespace
QTM_USE_NAMESPACE
这里的代码片段使用了一个 有预设字符的lineEdit来存放要发送的字符,可以是中文字符,这样做避免了关于字符编码的冗长讨论。实际操作中也不建议将中文字符硬编码在代码里,需要使用中文字符的地方应遵循Qt的国际化和本地化的操作。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessage>
// Use Qt Mobility API namespace
QTM_USE_NAMESPACE
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
service = new QMessageService(this);
connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(sendMessage()));
}
void MainWindow::sendMessage()
{
QMessage message;
QMessageAddressList toList;
QMessageAddress::Type addrType(QMessageAddress::Phone);
toList.append(QMessageAddress(addrType, ui->numberEdit->text()));//电话号码//这里可以设置群以号码
message.setType(QMessage::Sms);//短信类型
message.setTo(toList);
message.setBody(ui->messageEdit->text()); //设置短信内容为lineEdit中的字符,支持中文
service->send(message);
}
MainWindow::~MainWindow()
{
delete ui;
}
QMessageService 调用symbian的API发送短信,这里的构造函数使用AO异步实现,不能把这里的service做成局部变量,否则有可能应为构造没完成,导致调用send方法时出错,报“A data abort exception has occurred”
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageService>
QTM_USE_NAMESPACE
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void sendMessage();
private:
Ui::MainWindow *ui;
QMessageService *service;
};
#endif // MAINWINDOW_H

