QML SMShelper Plugin
This article creates a C++ extension to QML (SMSplugin) to expose Qt Messaging APIs to QML environment. Note it is also possible to launch the standard SMS editor from QML using Qt.openUrlExternally, as described in How to use Qt.openUrlExternally in QML.
Article Metadata
Code Example
Source file: Media:Smssender.zip
Tested with
SDK: Qt SDK 4.7.1 or newer and Qt mobility 1.1.0 or newer
Devices(s): N8, E7, C7
Compatibility
Platform(s): Symbian^3
Platform Security
Signing Required: Self-Signed
Capabilities: NetworkServices
Article
Keywords: QMessageService, QMessageManager
Created: jahartik
(29 Apr 2011)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Pro file
The following capabilities and libraries are required:
symbian:TARGET.CAPABILITY = NetworkServices
CONFIG += mobility
MOBILITY = messaging
Header file
smshelper.h applying this macro to definitions of member functions to allow them to be invoked via the meta-object system
#ifndef SMSHELPER_H
#define SMSHELPER_H
#include <QObject>
#include <qmessage.h>
#include <qmessageservice.h>
QTM_USE_NAMESPACE
#ifdef Q_OS_SYMBIAN
# define SENDSMS_ENABLED
#elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
# define SENDSMS_ENABLED
#endif
class SMSHelper : public QObject
{
Q_OBJECT
public:
explicit SMSHelper(QObject *parent = 0);
~SMSHelper();
Q_INVOKABLE bool sendsms(QString phonenumber, QString message);
signals:
void stateMsg(const QString &statemsg);
void errorMsg(const QString &errormsg);
void debugMsg(const QString &dbgmsg);
private slots:
void messageStateChanged(QMessageService::State s);
#ifndef SENDSMS_ENABLED
void signalFinishedState() { emit stateMsg("FinishedState");};
#endif
private:
QMessageService iMessageService;
QMessageManager iManager;
QMessageService::State state;
};
#endif // SMSHELPER_H
Source file
smshelper source code
#include "smshelper.h"
#ifndef SENDSMS_ENABLED
#include <QTimer>
#endif
SMSHelper::SMSHelper(QObject *parent) :
QObject(parent)
{
state = QMessageService::InactiveState;
connect(&iMessageService, SIGNAL(stateChanged(QMessageService::State)), this, SLOT(messageStateChanged(QMessageService::State)));
}
SMSHelper::~SMSHelper()
{
}
bool SMSHelper::sendsms(QString phonenumber, QString message)
{
#ifdef SENDSMS_ENABLED
if (!QMessageAccount::defaultAccount(QMessage::Sms).isValid())
{
emit errorMsg("No messageaccount for sms sending.");
return false;
}
if (state == QMessageService::InactiveState || state == QMessageService::FinishedState)
{
QMessage sms;
sms.setType(QMessage::Sms);
sms.setParentAccountId(QMessageAccount::defaultAccount(QMessage::Sms));
sms.setTo(QMessageAddress(QMessageAddress::Phone, phonenumber));
sms.setBody(message);
return iMessageService.send(sms);
}
else
{
return false;
}
#else
QTimer::singleShot(1000,this,SLOT(signalFinishedState()));
return true;
#endif
}
void SMSHelper::messageStateChanged(QMessageService::State s)
{
state = s;
if (s == QMessageService::InactiveState)
{
emit stateMsg("InactiveState");
}
else if (s == QMessageService::ActiveState)
{
emit stateMsg("ActiveState");
}
else if (s == QMessageService::CanceledState)
{
emit stateMsg("CanceledState");
}
else if (s == QMessageService::FinishedState)
{
emit stateMsg("FinishedState");
}
else
{
emit stateMsg(QString("QMessageService::%1").arg(s));
}
}
To register the C++ type in the QML system with the name SMSHelper include following code in your main.cpp file
qmlRegisterType<SMSHelper>("SMSHelper",1,0,"SMSHelper");
Import sms plugin in QML
import SMSHelper 1.0Example use QML
SMSHelper
{
id: smshelper
}


Hi, Can you please attache source code file with the article ?
Contents
Biskero -
Yes, a working source code would be great since getting some errors while trying to compile this codebiskero 22:03, 8 November 2011 (EET)
Texrat - smshelper source code
I'm new to Qt. I don't know how to implement the actual smshelper source code. I see instructions for the header file, and main.cpp, but what about the source file itself? Where does it go, what is it named?Texrat 19:30, 29 November 2011 (EET)
NielsMayer - I'm trying to solve a similar issue for VoiceToGoog, here's my findings
http://code.google.com/p/voicetogoog/issues/detail?id=1NielsMayer 20:43, 29 November 2011 (EET)
Vineet.jain - Example Project
I have attached a sample project named Smssender . It contains the code for entering a text in the text box & clicking a button 'Create SMS' will open the phone's default sms editor with the text as entered in the textbox & contact to whom sms is to be send is to be chosen by the user from the contact book.vineet.jain 14:14, 12 December 2011 (EET)
Hamishwillee - Vineet.jain - Example Project - thank you!
Hi Vineet
Thanks very much for the example project. A couple of questions.
I've added the SMS category
Cheers
Hhamishwillee 01:25, 13 December 2011 (EET)
Vineet.jain - Clarification about the Project
Hello Hamish , let me clarify with the questions you have asked:
1) The example project i have attached does not include the "Example use QML" section(nor" Import sms plugin in QML" section), the way i have linked the C++ code to QML is bit different.
2) I have tested the code using the Qt SDK 1.1.3 , so i hope it should work on the latest Qt SDK 1.1.4.
3) I have build-ed/tested this project for MeeGo platform only(tested on N9 & N950 ), no Symbian dependency/requirement is there.
Also , the example project i have attached is not completely like the code given above, though its quite same but there are some differences, but the end result of course is same(sending the sms)
Vineetvineet.jain 09:28, 15 December 2011 (EET)
Hamishwillee - Vineet - thanks for the clarification
Hi Vineet
Thanks very much for the clarification.
Presumably your code improves over what is here - so perhaps worth copying that in place. In any case, its worth having a section describing the differences between your code and that displayed so people don't get confused.
Regards
Hamishhamishwillee 02:06, 16 December 2011 (EET)