×
Namespaces

Variants
Actions
(Difference between revisions)

使用Qt Mobility Messaging API发送短信

From Nokia Developer Wiki
Jump to: navigation, search
flycarl (Talk | contribs)
flycarl (Talk | contribs)
Line 52: Line 52:
 
这里的代码片段使用了一个 有预设字符的lineEdit来存放要发送的字符,可以是中文字符,这样做避免了关于字符编码的冗长讨论。实际操作中也不建议将中文字符硬编码在代码里,需要使用中文字符的地方应遵循[[Qt的国际化和本地化]]的操作。
 
这里的代码片段使用了一个 有预设字符的lineEdit来存放要发送的字符,可以是中文字符,这样做避免了关于字符编码的冗长讨论。实际操作中也不建议将中文字符硬编码在代码里,需要使用中文字符的地方应遵循[[Qt的国际化和本地化]]的操作。
 
<code cpp>
 
<code cpp>
 +
#include "mainwindow.h"
 +
#include "ui_mainwindow.h"
 +
#include <QMessage>
 +
// Use Qt Mobility API namespace
 +
QTM_USE_NAMESPACE
  
QMessageService service;
+
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()));
 +
}
  
QMessage message;
+
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);
 +
}
  
QMessageAddressList toList;
+
MainWindow::~MainWindow()
 +
{
 +
    delete ui;
 +
}
 +
</code>
 +
<code h>
 +
#ifndef MAINWINDOW_H
 +
#define MAINWINDOW_H
  
QMessageAddress::Type addrType(QMessageAddress::Phone);
+
#include <QMainWindow>
 +
#include <QMessageService>
  
toList.append(QMessageAddress(addrType, “13800000000”);//电话号码//这里可以设置群以号码
+
QTM_USE_NAMESPACE
  
message.setType(QMessage::Sms);//短信类型
+
namespace Ui {
 +
    class MainWindow;
 +
}
  
message.setTo(toList);
 
  
message.setBody(ui->lineEdit->text()); //设置短信内容为lineEdit中的字符,支持中文
 
  
service.send(message);
+
class MainWindow : public QMainWindow
 +
{
 +
    Q_OBJECT
  
</code>
+
public:
 +
    explicit MainWindow(QWidget *parent = 0);
 +
    ~MainWindow();
 +
public slots:
 +
    void sendMessage();
  
 +
private:
 +
    Ui::MainWindow *ui;
 +
    QMessageService *service;
 +
};
 +
 +
#endif // MAINWINDOW_H
 +
</code>
 
==下载Demo==
 
==下载Demo==
  

Revision as of 10:18, 18 November 2010

这演示如何用Qt Mobility的Messaging API发送短信,

Article Metadata
Tested with
Devices(s): Nokia 5800 and N8.
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition Symbian^3
Symbian
Article
Keywords: QMessageService, QMessage, Chinese
Created: (18 Nov 2010)
Last edited: flycarl (18 Nov 2010)


Contents

简介

这里的代码演示如何用Qt Mobility的Messaging API发送短信,自Qt Mobility 1.1.0可以发送中文短信。 代码需要以下能力 NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData 。自签名不够用,至少需要 开发者签名 ,也可以用 Open Signed Online来测试。


准备条件

编辑pro文件

在pro文件中增加这些行

CONFIG += mobility
MOBILITY = messaging
 
symbian {
 
TARGET.CAPABILITY = NetworkServices \
LocalServices \
ReadUserData \
WriteUserData \
UserEnvironment \
ReadDeviceData \
WriteDeviceData
}

代码

下面的代码片段演示了如何发短信,当使用Mobility API 头文件时,需要使用QTM_USE_NAMESPACE,否则会找不到头文件;当前置声明时,需要用 QTM_BEGIN_NAMESPACEQTM_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;
}

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


#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

下载Demo

参考链接

234 page views in the last 30 days.