×
Namespaces

Variants
Actions

Receiving an SMS in Qt

From Nokia Developer Wiki
Jump to: navigation, search

This code snippet demonstrates how to listen for incoming SMS messages in Qt. The snippet uses the Qt Mobility Messaging module to show the received message to the user and then to remove it from the inbox folder.

Article Metadata
Tested with
SDK: Qt Mobility 1.0.0
Devices(s): Nokia N97, Nokia N900
Compatibility
Platform(s): S60 5th Edition
Maemo 5
Symbian
S60 5th Edition
Platform Security
Signing Required: DevCert
Capabilities: ReadUserData WriteUserData ReadDeviceData WriteDeviceData
Article
Keywords: QMessage, QMessageAddress, QMessageAccountId, QMessageService
Created: tepaa (21 May 2010)
Last edited: hamishwillee (17 Oct 2012)

Contents

Qt project file

Link the Messaging Qt Mobility module into project.

CONFIG += mobility
MOBILITY = messaging

Using Messaging requires the ReadDeviceData and WriteDeviceData capabilities from Open Signed Online. For more information, see the User guide: Symbian Signed.

symbian {
TARGET.CAPABILITY = ReadUserData \
WriteUserData \
ReadDeviceData \
WriteDeviceData
}

Header

#include <QMainWindow>
#include <QPointer>
#include <QMessageBox>
 
#include <qmessage.h>
#include <qmessagemanager.h>
 
// QtMobility namespace
QTM_USE_NAMESPACE
 
class qtSnippets : public QMainWindow
{
Q_OBJECT
 
public:
qtSnippets(QWidget *parent = 0);
 
private slots:
void processIncomingSMS();
// Listening signals from QMessageManager
void messageAdded(const QMessageId&,
const QMessageManager::NotificationFilterIdSet&);
 
private:
QPointer<QMessageManager> m_manager;
QMessageManager::NotificationFilterIdSet m_notifFilterSet;
QMessageId m_messageId;
};

Source

#include <QTimer>
 
qtSnippets::qtSnippets(QWidget *parent)
: QMainWindow(parent)
{
// Manager for listening messages
m_manager = new QMessageManager(this);
 
// Listen new added messages
connect(m_manager, SIGNAL(messageAdded(const QMessageId&,
const QMessageManager::NotificationFilterIdSet&)),
this, SLOT(messageAdded(const QMessageId&,
const QMessageManager::NotificationFilterIdSet&)));
 
// Create 2 filers set for filtering messages
// - SMS filter
// - InboxFolder filter
m_notifFilterSet.insert(m_manager->registerNotificationFilter(
QMessageFilter::byType(QMessage::Sms) &
QMessageFilter::byStandardFolder(QMessage::InboxFolder)));
}
 
void qtSnippets::messageAdded(const QMessageId& id,
const QMessageManager::NotificationFilterIdSet& matchingFilterIds)
{
// Message added...
if (matchingFilterIds.contains(m_notifFilterSet)) {
// ...and it fits into our filters, lets process it
m_messageId = id;
QTimer::singleShot(0, this, SLOT(processIncomingSMS()));
}
}
 
void qtSnippets::processIncomingSMS()
{
QMessage message = m_manager->message(m_messageId);
// SMS message body
QString messageString = message.textContent();
// Show message to user
QMessageBox::information(this, "Incoming SMS", messageString);
// Remove message from inbox
m_manager->removeMessage(m_messageId);
}

Postconditions

An SMS message has been received and shown to the user, and then removed from the inbox folder.

See also

This page was last modified on 17 October 2012, at 09:20.
87 page views in the last 30 days.