×
Namespaces

Variants
Actions
(Difference between revisions)

QML SMShelper Plugin

From Nokia Developer Wiki
Jump to: navigation, search
kpakarin (Talk | contribs)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(17 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:Qt]][[Category:Qt Mobility]][[Category:Qt Quick]][[Category:Symbian]]
+
[[Category:Qt]][[Category:Qt Mobility]][[Category:Qt Quick]][[Category:SMS]][[Category:Code Examples]][[Category:MeeGo Harmattan]]
__NOTOC__
+
{{Abstract|This article creates a C++ extension to QML ({{Icode|SMSplugin}}) to expose [http://doc.qt.nokia.com/qtmobility/qtmessaging.html Qt Messaging APIs] to QML environment.}}  Note it is also possible to launch the standard SMS editor from QML using [http://doc.qt.nokia.com/stable/qml-qt.html#openUrlExternally-method Qt.openUrlExternally], as described in [[How to use Qt.openUrlExternally in QML]].
__NOEDITSECTION__
+
 
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=...
+
|sourcecode= [[Media:Smssender.zip]]
|platform=Symbian 3
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices=N8, E7, C7  
+
|devices= N8, E7, C7
|category=...
+
|sdk= [http://qt.nokia.com/downloads Qt SDK] 4.7.1 or newer and [http://qt.nokia.com/products/qt-addons/mobility Qt mobility] 1.1.0 or newer
|subcategory=...
+
|platform= Symbian^3
|creationdate=29.04.2011
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|keywords=QML, SMS, Messaging
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= Self-Signed
 +
|capabilities= NetworkServices
 +
|keywords= QMessageService, QMessageManager
 +
|language= <!-- Language category code for non-English topics - e.g. 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= 20110429
 +
|author= [[User:Jahartik]]
 
}}
 
}}
 
 
 
 
''Delete the guidance text written in italics.''
 
 
==Overview==
 
 
SMSplugin to expose Qt mobility SMS apis to Qml environment
 
 
This snippet can be self-signed.
 
 
==Preconditions==
 
 
Qt 4.7.1 or newer and Qt mobility 1.1.0 or newer installed
 
  
 
==Pro file==
 
==Pro file==
  
 
The following capabilities and libraries are required:
 
The following capabilities and libraries are required:
 
+
<code cpp-qt>
CAPABILITY
+
 
symbian:TARGET.CAPABILITY = NetworkServices
 
symbian:TARGET.CAPABILITY = NetworkServices
 
 
CONFIG += mobility
 
CONFIG += mobility
 
 
MOBILITY = messaging
 
MOBILITY = messaging
 
+
</code>
 
==Header file==
 
==Header file==
  
smshelper.h applying this macro to definitions of member functions to allow them to be invoked via the meta-object system  
+
'''smshelper.h''' applying this macro to definitions of member functions to allow them to be invoked via the''' meta-object system'''
  
<code>
+
<code cpp-qt>
 
#ifndef SMSHELPER_H
 
#ifndef SMSHELPER_H
 
#define SMSHELPER_H
 
#define SMSHELPER_H
Line 83: Line 78:
 
     QMessageManager iManager;
 
     QMessageManager iManager;
 
     QMessageService::State state;
 
     QMessageService::State state;
 
 
 
};
 
};
  
 
#endif // SMSHELPER_H
 
#endif // SMSHELPER_H
 
 
</code>  
 
</code>  
  
Line 95: Line 87:
  
 
smshelper source code  
 
smshelper source code  
<code>
+
<code cpp-qt>
 
#include "smshelper.h"
 
#include "smshelper.h"
  
Line 107: Line 99:
 
     state = QMessageService::InactiveState;
 
     state = QMessageService::InactiveState;
 
     connect(&iMessageService, SIGNAL(stateChanged(QMessageService::State)), this, SLOT(messageStateChanged(QMessageService::State)));
 
     connect(&iMessageService, SIGNAL(stateChanged(QMessageService::State)), this, SLOT(messageStateChanged(QMessageService::State)));
 
 
}
 
}
  
Line 113: Line 104:
 
{
 
{
 
}
 
}
 
  
  
Line 146: Line 136:
 
     return true;
 
     return true;
 
#endif
 
#endif
 
 
 
 
}
 
}
  
Line 176: Line 163:
 
     }
 
     }
 
}
 
}
 
 
 
</code>  
 
</code>  
  
 
To register the C++ type in the QML system with the name SMSHelper include following code in your main.cpp file
 
To register the C++ type in the QML system with the name SMSHelper include following code in your main.cpp file
  
<code>  
+
<code cpp-qt>  
 
qmlRegisterType<SMSHelper>("SMSHelper",1,0,"SMSHelper");
 
qmlRegisterType<SMSHelper>("SMSHelper",1,0,"SMSHelper");
 
</code>  
 
</code>  
  
 
Import sms plugin in QML
 
Import sms plugin in QML
<code>  
+
<code cpp-qt>  
 
import SMSHelper 1.0
 
import SMSHelper 1.0
 
</code>  
 
</code>  
  
 
Example use QML
 
Example use QML
<code>  
+
<code css>  
 
SMSHelper
 
SMSHelper
 
     {
 
     {
 
         id: smshelper
 
         id: smshelper
----
+
}
smshelper.sendsms(phone, message)
+
</code>
 +
 
 +
== SMShelper developer APIs ==
 +
 
 +
<code cpp-qt>
 +
//Constructor and destructor
 +
SMSHelper(QObject *parent = 0);
 +
~SMSHelper();
 +
 
 +
public slots:
 +
sendsms(QString phonenumber, QString message);
 +
 
 +
signals:
 +
void stateMsg(const QString &statemsg);
 +
void errorMsg(const QString &errormsg);
 +
void debugMsg(const QString &dbgmsg);
 
</code>
 
</code>

Latest revision as of 04:18, 11 October 2012

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

[edit] Pro file

The following capabilities and libraries are required:

symbian:TARGET.CAPABILITY = NetworkServices
CONFIG += mobility
MOBILITY = messaging

[edit] 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


[edit] 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.0

Example use QML

SMSHelper
{
id: smshelper
}

[edit] SMShelper developer APIs

//Constructor and destructor
SMSHelper(QObject *parent = 0);
~SMSHelper();
 
public slots:
sendsms(QString phonenumber, QString message);
 
signals:
void stateMsg(const QString &statemsg);
void errorMsg(const QString &errormsg);
void debugMsg(const QString &dbgmsg);
This page was last modified on 11 October 2012, at 04:18.
123 page views in the last 30 days.
×