×
Namespaces

Variants
Actions
(Difference between revisions)

Read and send SMS messages using the Qt Messaging API

From Nokia Developer Wiki
Jump to: navigation, search
Yashu (Talk | contribs)
m
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{CodeSnippet
+
[[Category:Messaging]][[Category:Qt Mobility]][[Category:Code Examples]][[Category:MeeGo Harmattan]][[Category:Symbian]][[Category:SMS]][[Category:Qt C++ UI]]
|id=...
+
{{Abstract|This code example shows how to use the [http://doc.qt.nokia.com/qtmobility-1.1/messaging.html Qt Messaging API] to read and send SMS messages.}}
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition
+
{{ArticleNeedsUpdate|timestamp=20120216034336|user=[[User:Hamishwillee|&lt;br /&gt;----]]|Usage of Qt Mobility API is through QWidget app framework. Should be updated to be accessed through Qt Quick.}}
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= [[Media:QtMessaging.zip]]
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= Nokia 5800 and Nokia N97.
 
|devices= Nokia 5800 and Nokia N97.
|category=Qt for Symbian
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|subcategory= Qt Mobility API
+
|platform= S60 3rd Edition, FP1, FP2<br>S60 5th Edition
|creationdate=March 30, 2010
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= NetworkServices, LocalServices, ReadUserData, WriteUserData, UserEnvironment, ReadDeviceData, WriteDeviceData
 
|keywords= QMessageServiceAction, QMessageId
 
|keywords= QMessageServiceAction, QMessageId
 +
|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= 20100329
 +
|author= [[User:Yashu]]
 
}}
 
}}
 
 
==Overview==
 
 
This code example shows how to use messagning API in Qt for symbian. Code demonstrate how to read all messages of each folders and how to send a message. Code example uses Messaging APIs. Purpose of this code example is to show how to use Qt and QtMoblity APIs to read and send messages.
 
 
This snippet does  require NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData capabilities. Self-signing is not possible and atleast [[Developer certificate]] is needed.
 
 
 
==Prerequisite==
 
 
* Install Qt for Symbian:[[Installing Qt on Symbian]]
 
* Install QtMobility APIs:[[Setting up environment for Qt Mobility API]]
 
 
  
 
==Headers required==
 
==Headers required==
<code cpp>
+
<code cpp-qt>
 +
/* This include is not portable and should be avoided */
 
#include "qtmessaging.h"
 
#include "qtmessaging.h"
 
</code>
 
</code>
Line 31: Line 34:
 
Modify your .pro file as follows.
 
Modify your .pro file as follows.
  
<code cpp>
+
<code cpp-qt>
 
CONFIG += mobility
 
CONFIG += mobility
 
MOBILITY = messaging
 
MOBILITY = messaging
INCLUDEPATH += D:/QtMobility/src/messaging
 
INCLUDEPATH += D:/QtMobility/src/global
 
 
/* To show icon on list box */
 
/* To show icon on list box */
 
RESOURCES += Messages.qrc
 
RESOURCES += Messages.qrc
Line 51: Line 52:
 
Add .qrc file to project to show icon in list widget.
 
Add .qrc file to project to show icon in list widget.
  
<code cpp>
+
<code cpp-qt>
 
<!DOCTYPE RCC><RCC version="1.0">
 
<!DOCTYPE RCC><RCC version="1.0">
 
  <qresource>
 
  <qresource>
Line 60: Line 61:
 
==Header files==
 
==Header files==
  
<code cpp>
+
<code cpp-qt>
 
/* Messages.h*/
 
/* Messages.h*/
 
#ifndef MESSAGING_H
 
#ifndef MESSAGING_H
Line 66: Line 67:
  
 
#include <QWidget>
 
#include <QWidget>
 +
/* This include is not portable */
 
#include "qtmessaging.h"
 
#include "qtmessaging.h"
  
Line 116: Line 118:
 
</code>
 
</code>
  
<code cpp>
+
 
 +
 
 +
<code cpp-qt>
 
/* MainPage.h */
 
/* MainPage.h */
 
#ifndef MAINPAGE_H
 
#ifndef MAINPAGE_H
Line 122: Line 126:
  
 
#include <QtGui/QMainWindow>
 
#include <QtGui/QMainWindow>
 +
/* This include is not portable */
 
#include "qtmessaging.h"
 
#include "qtmessaging.h"
  
Line 178: Line 183:
  
  
<code cpp>
+
 
 +
 
 +
<code cpp-qt>
 
/* ListBox.h */
 
/* ListBox.h */
 
#ifndef LISTBOX_H
 
#ifndef LISTBOX_H
Line 220: Line 227:
 
</code>
 
</code>
  
<code cpp>
+
 
 +
 
 +
 
 +
<code cpp-qt>
 
/* SendSMS.h */
 
/* SendSMS.h */
 
#ifndef SENDSMS_H
 
#ifndef SENDSMS_H
Line 226: Line 236:
  
 
#include <QWidget>
 
#include <QWidget>
 +
/* This include is not portable */
 
#include "qtmessaging.h"
 
#include "qtmessaging.h"
  
Line 278: Line 289:
 
==Source files==
 
==Source files==
  
<code cpp>
+
<code cpp-qt>
 
/* Messages.cpp*/
 
/* Messages.cpp*/
 
#include "MainPage.h"
 
#include "MainPage.h"
Line 284: Line 295:
 
#include "ListBox.h"
 
#include "ListBox.h"
 
#include "Messaging.h"
 
#include "Messaging.h"
#include <qmessagestore.h>
+
#include <QMessageStore>
 
#include <QStackedWidget>
 
#include <QStackedWidget>
 
#include <QVBoxLayout>
 
#include <QVBoxLayout>
Line 475: Line 486:
 
</code>
 
</code>
  
<code cpp>
+
 
 +
 
 +
 
 +
<code cpp-qt>
 
/* MainPage.cpp */
 
/* MainPage.cpp */
 
#include "MainPage.h"
 
#include "MainPage.h"
Line 652: Line 666:
  
  
<code cpp>
+
 
 +
 
 +
<code cpp-qt>
 
/* ListBox.cpp */
 
/* ListBox.cpp */
 
#include "ListBox.h"
 
#include "ListBox.h"
Line 755: Line 771:
 
</code>
 
</code>
  
<code cpp>
+
 
 +
 
 +
 
 +
<code cpp-qt>
 
/* SendSMS.cpp */
 
/* SendSMS.cpp */
 
#include "SendSMS.h"
 
#include "SendSMS.h"
Line 861: Line 880:
 
if (!text.isEmpty())  
 
if (!text.isEmpty())  
 
{
 
{
ret = QMessageBox::question(0, "Text message", "Do you want to save message to draft??" , QMessageBox::Ok , QMessageBox::No);
+
ret = QMessageBox::question(0, "Text message", "Do you want to save message to draft??" , QMessageBox::Ok, QMessageBox::No);
 
}
 
}
 
 
Line 886: Line 905:
 
     <td align="left">
 
     <td align="left">
 
'''Display all messaging folders'''<br>                     
 
'''Display all messaging folders'''<br>                     
[[Image:QtMessagesMain.jpg]]
+
[[File:QtMessagesMain.jpg]]
 
   </td>
 
   </td>
 
     <td align="left">
 
     <td align="left">
 
''' Create and send new message'''<br>                     
 
''' Create and send new message'''<br>                     
[[Image:QtMessagesCompose.jpg]]
+
[[File:QtMessagesCompose.jpg]]
 
   </td>
 
   </td>
 
   </tr>
 
   </tr>
Line 899: Line 918:
 
     <td align="left">
 
     <td align="left">
 
'''Show inbox folder'''<br>                     
 
'''Show inbox folder'''<br>                     
[[Image:QtMessagesInBox.jpg]]
+
[[File:QtMessagesInBox.jpg]]
 
   </td>
 
   </td>
 
     <td align="left">
 
     <td align="left">
 
''' Read selected message from inbox'''<br>                     
 
''' Read selected message from inbox'''<br>                     
[[Image:QtMessagesReadItem.jpg]]
+
[[File:QtMessagesReadItem.jpg]]
 
   </td>
 
   </td>
 
   </tr>
 
   </tr>
Line 912: Line 931:
 
     <td align="left">
 
     <td align="left">
 
'''Show sent item folder'''<br>                     
 
'''Show sent item folder'''<br>                     
[[Image:QtMessagesSentItem.jpg]]
+
[[File:QtMessagesSentItem.jpg]]
 
   </td>
 
   </td>
 
   </tr>
 
   </tr>
Line 920: Line 939:
  
 
* Working [[Media:QtMessaging.zip|Code Example]] is available to download from here.
 
* Working [[Media:QtMessaging.zip|Code Example]] is available to download from here.
 
[[Category:Qt]][[Category:Qt for Symbian]][[Category:Code Examples]]
 

Revision as of 04:13, 11 October 2012

This code example shows how to use the Qt Messaging API to read and send SMS messages.

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (16 Feb 2012)
Usage of Qt Mobility API is through QWidget app framework. Should be updated to be accessed through Qt Quick.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 and Nokia N97.
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Platform Security
Capabilities: NetworkServices, LocalServices, ReadUserData, WriteUserData, UserEnvironment, ReadDeviceData, WriteDeviceData
Article
Keywords: QMessageServiceAction, QMessageId
Created: Yashu (29 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)

Contents

Headers required

/* This include is not portable and should be avoided */
#include "qtmessaging.h"

.pro file

Modify your .pro file as follows.

CONFIG += mobility
MOBILITY = messaging
/* To show icon on list box */
RESOURCES += Messages.qrc
symbian:TARGET.CAPABILITY = NetworkServices \
LocalServices \
ReadUserData \
WriteUserData \
UserEnvironment \
ReadDeviceData \
WriteDeviceData

Add resource file

Add .qrc file to project to show icon in list widget.

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>rsc/Inbox.jpg</file>
</qresource>
</RCC>

Header files

/* Messages.h*/
#ifndef MESSAGING_H
#define MESSAGING_H
 
#include <QWidget>
/* This include is not portable */
#include "qtmessaging.h"
 
QT_BEGIN_NAMESPACE
class QStackedWidget;
QT_END_NAMESPACE
 
QTM_USE_NAMESPACE;
 
/* forward declaration */
class MainPage;
class SendSMS;
class ListBox;
 
class Messaging : public QWidget
{
Q_OBJECT
 
public:
Messaging(QWidget *parent = 0);
~Messaging();
 
public slots:
void activateSendSMS();
void activateMainPage();
void activateInbox();
void activateSentItem();
void activateDraft();
void activateOutbox();
void messageServiceStateChanged(QMessageServiceAction::State s);
 
 
private:
/* The stacked widget which will display the pages */
QStackedWidget *m_stackedWidget;
 
/* Widget to show main page/each folder in messaging */
MainPage *m_mainPage;
/* widget to create new message */
SendSMS *m_sendSMS;
/*Widget to show each message of a folder */
ListBox *m_listBox;
 
QMessageId messageId;
QMessageServiceAction *messageService;
};
 
#endif // MESSAGING_H


/* MainPage.h */
#ifndef MAINPAGE_H
#define MAINPAGE_H
 
#include <QtGui/QMainWindow>
/* This include is not portable */
#include "qtmessaging.h"
 
QT_BEGIN_NAMESPACE
class QListWidget;
class QScrollArea;
class QListWidgetItem;
class QAction;
QT_END_NAMESPACE
 
QTM_USE_NAMESPACE;
 
class MainPage : public QMainWindow
{
Q_OBJECT
 
public:
MainPage(QWidget *parent = 0);
~MainPage();
 
private:
void countMessagesInFolders();
 
signals:
void createMessage();
void createInbox();
void createSentItem();
void createDraft();
void createOutbox();
 
public slots:
void getSelectedItem(QListWidgetItem* item);
void getCurrentRow(int a);
void openItem();
 
 
private:
QListWidget *listWidget;
QScrollArea *scrollArea;
int selectedRow;
int previousRow;
 
//Actions for menu item
QAction* rightSoftKeyAction;
 
 
int countInboxSMS;
int countSentSMS;
int countDraftSMS;
int countOutboxSMS;
};
 
#endif // MAINPAGE_H



/* ListBox.h */
#ifndef LISTBOX_H
#define LISTBOX_H
 
#include <QtGui/QMainWindow>
 
class QListWidget;
class QScrollArea;
class QListWidgetItem;
 
class ListBox : public QMainWindow
{
Q_OBJECT
 
public:
ListBox(QWidget *parent = 0);
~ListBox();
 
signals:
void gotoMainPage();
 
public slots:
void getSelectedItem(QListWidgetItem* item);
void getCurrentRow(int a);
void closeListBox();
void openItem();
void setItemText(const QStringList& stringList);
 
private:
QAction *rightSoftKeyAction;
QAction *leftSoftKeyAction;
QListWidget *listWidget;
QScrollArea *scrollArea;
int selectedRow;
int previousRow;
};
 
#endif // ListBox_H



/* SendSMS.h */
#ifndef SENDSMS_H
#define SENDSMS_H
 
#include <QWidget>
/* This include is not portable */
#include "qtmessaging.h"
 
QT_BEGIN_NAMESPACE
class QAction;
class QTextEdit;
class QLabel;
class QLineEdit;
class QGridLayout;
class QVBoxLayout;
class QListWidget;
QT_END_NAMESPACE
 
QTM_USE_NAMESPACE;
 
class SendSMS : public QWidget
{
Q_OBJECT
 
public:
SendSMS(QWidget *parent = 0);
~SendSMS();
 
void setMessageServiceAction(QMessageServiceAction *messageService);
 
signals:
void gotoMainPage();
 
private slots:
void sendMessage();
void closeEditor();
 
 
private:
QAction *softKeyAction;
QAction *leftSoftKeyAction;
QLineEdit *mobileNumberInput;
QTextEdit *messageBodyInput;
QLabel *labelTo;
 
QGridLayout *layout;
QVBoxLayout *mainLayout;
 
QMessageId messageId;
QMessageServiceAction *messageService;
};
 
#endif // SENDSMS_H

Source files

/* Messages.cpp*/
#include "MainPage.h"
#include "SendSMS.h"
#include "ListBox.h"
#include "Messaging.h"
#include <QMessageStore>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QMessageBox>
 
Messaging::Messaging(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("Messaging"));
 
/* initialize main page widget*/
m_mainPage = new MainPage(this);
connect(m_mainPage, SIGNAL(createMessage()), this, SLOT(activateSendSMS()));
connect(m_mainPage, SIGNAL(createInbox()), this, SLOT(activateInbox()));
connect(m_mainPage, SIGNAL(createSentItem()), this, SLOT(activateSentItem()));
connect(m_mainPage, SIGNAL(createDraft()), this, SLOT(activateDraft()));
connect(m_mainPage, SIGNAL(createOutbox()), this, SLOT(activateOutbox()));
 
/* initialize widget to send SMS */
m_sendSMS = new SendSMS(this);
connect(m_sendSMS, SIGNAL(gotoMainPage()), this, SLOT(activateMainPage()));
 
/* initialize list widget */
m_listBox = new ListBox(this);
 
connect(m_listBox, SIGNAL(gotoMainPage()), this, SLOT(activateMainPage()));
 
m_stackedWidget = new QStackedWidget(this);
m_stackedWidget->addWidget(m_mainPage);
m_stackedWidget->addWidget(m_sendSMS);
m_stackedWidget->addWidget(m_listBox);
m_stackedWidget->setCurrentIndex(0);
 
 
QVBoxLayout *centralLayout = new QVBoxLayout;
centralLayout->addWidget(m_stackedWidget);
 
/* The QWidget will take ownership of centralLayout. So no need to delete centralLayout*/
setLayout(centralLayout);
 
messageService = new QMessageServiceAction(this);
connect(messageService, SIGNAL(stateChanged(QMessageServiceAction::State)), this, SLOT(messageServiceStateChanged(QMessageServiceAction::State)));
 
}
 
/* Activate SensSMS widget */
void Messaging::activateSendSMS()
{
setWindowTitle(tr("Text message"));
m_sendSMS->setMessageServiceAction(messageService);
m_stackedWidget->setCurrentIndex(1);
}
 
/*Activate main page widget */
void Messaging::activateMainPage()
{
setWindowTitle(tr("Messaging"));
m_stackedWidget->setCurrentIndex(0);
}
 
/* activate widget to show inbox */
void Messaging::activateInbox()
{
setWindowTitle(tr("Inbox"));
 
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::InboxFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList messageIdList(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
QMessage message;
 
QStringList itemList;
 
for(int i = 0; i < messageIdList.count(); ++i)
{
message = QMessageStore::instance()->message(messageIdList.at(i));
QMessageAddress from = message.from();
QString number(from.recipient());
QString text;
text.append( number + ": " + message.find(message.bodyId()).textContent());
itemList.append(text);
}
m_listBox->setItemText(itemList);
m_stackedWidget->setCurrentIndex(2);
}
 
/* activate widget to show sent item */
void Messaging::activateSentItem()
{
setWindowTitle(tr("Sent"));
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::SentFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList messageIdList(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
QMessage message;
 
QStringList itemList;
 
for(int i = 0; i < messageIdList.count(); ++i)
{
message = QMessageStore::instance()->message(messageIdList.at(i));
QMessageAddress from = message.from();
QString number(from.recipient());
QString text;
text.append( number + ": " + message.find(message.bodyId()).textContent());
itemList.append(text);
}
m_listBox->setItemText(itemList);
m_stackedWidget->setCurrentIndex(2);
}
 
/* activate widget to show draft */
void Messaging::activateDraft()
{
setWindowTitle(tr("Draft"));
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::DraftsFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList messageIdList(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
QMessage message;
 
QStringList itemList;
 
for(int i = 0; i < messageIdList.count(); ++i)
{
message = QMessageStore::instance()->message(messageIdList.at(i));
QMessageAddress from = message.from();
QString number(from.recipient());
QString text;
text.append( number + ": " + message.find(message.bodyId()).textContent());
itemList.append(text);
}
m_listBox->setItemText(itemList);
m_stackedWidget->setCurrentIndex(2);
}
 
/* activate widget to show sent outbox */
void Messaging::activateOutbox()
{
setWindowTitle(tr("Outbox"));
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::OutboxFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
 
const QMessageIdList messageIdList(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
QMessage message;
 
QStringList itemList;
 
for(int i = 0; i < messageIdList.count(); ++i)
{
message = QMessageStore::instance()->message(messageIdList.at(i));
QMessageAddress from = message.from();
QString number(from.recipient());
QString text;
text.append( number + ": " + message.find(message.bodyId()).textContent());
itemList.append(text);
}
m_listBox->setItemText(itemList);
m_stackedWidget->setCurrentIndex(2);
}
 
 
void Messaging::messageServiceStateChanged(QMessageServiceAction::State s)
{
if (s == QMessageServiceAction::Successful)
{
QMessageBox::information(0, "Message sent", "Message sent successfully");
}
else if (s == QMessageServiceAction::Failed)
{
QMessageBox::warning(0, "Failed", "Unable to send message");
 
if (!QMessageStore::instance()->removeMessage(messageId))
{
QMessageBox::warning(0, "Unable to remove", "Unable to remove message: " + messageId.toString());
}
messageId = QMessageId();
}
}
 
Messaging::~Messaging()
{
}



/* MainPage.cpp */
#include "MainPage.h"
#include <QListWidget>
#include <QScrollArea>
#include <QMessageBox>
#include <QItemSelectionModel>
#include <QKeyEvent>
#include <QAction>
 
MainPage::MainPage(QWidget *parent)
: QMainWindow(parent)
{
countMessagesInFolders();
 
/* To show items in list view */
listWidget = new QListWidget();
listWidget->setObjectName(QString::fromUtf8("listWidget"));
 
/* To make UI scrollable */
scrollArea = new QScrollArea();
 
/* scrollArea takes ownership of the listWidget, so no need to delete it */
scrollArea->setWidget(listWidget);
scrollArea->setAlignment(Qt::AlignLeft);
scrollArea->setWidgetResizable(true);
 
/* QMainWindow takes ownership of the scrollArea, so no need to delete it */
setCentralWidget(scrollArea);
 
connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(getSelectedItem(QListWidgetItem*)));
connect(listWidget, SIGNAL(currentRowChanged (int)), this, SLOT(getCurrentRow(int)));
 
/* set font and icon size of listWiget */
QFont listWidgetFont("Courier", 8, QFont::Normal);
listWidget->setIconSize(QSize(60 ,70));
 
/* The listWidget will take ownership of the each QListWidgetItem item, so no need to delete it. */
QListWidgetItem* listItemNewMessage = new QListWidgetItem;
listItemNewMessage->setFont(listWidgetFont);
listItemNewMessage->setText("New message");
/* set list item icon */
listItemNewMessage->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemNewMessage->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemNewMessage);
 
QString listItemText;
QListWidgetItem* listItemInbox = new QListWidgetItem;
listItemInbox->setFont(listWidgetFont);
listItemText.setNum(countInboxSMS);
listItemText.prepend("Inbox ");
listItemText.append(" messages");
listItemInbox->setText(listItemText);
listItemInbox->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemInbox->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemInbox);
 
QListWidgetItem* listItemSent = new QListWidgetItem;
listItemSent->setFont(listWidgetFont);
listItemText.clear();
listItemText.setNum(countSentSMS);
listItemText.prepend("Sent ");
listItemText.append(" messages");
listItemSent->setText(listItemText);
listItemSent->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemSent->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemSent);
 
QListWidgetItem* listItemDraft = new QListWidgetItem;
listItemDraft->setFont(listWidgetFont);
listItemText.clear();
listItemText.setNum(countDraftSMS);
listItemText.prepend("Draft ");
listItemText.append(" messages");
listItemDraft->setText(listItemText);
listItemDraft->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemDraft->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemDraft);
 
QListWidgetItem* listItemOutbox = new QListWidgetItem;
listItemOutbox->setFont(listWidgetFont);
listItemText.clear();
listItemText.setNum(countOutboxSMS);
listItemText.prepend("Outbox ");
listItemText.append(" messages");
listItemOutbox->setText(listItemText);
listItemOutbox->setIcon(QIcon(":/rsc/Inbox.jpg"));
listItemOutbox->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItemOutbox);
 
 
listWidget->setCurrentRow(0,QItemSelectionModel::Select);
 
selectedRow = previousRow = 0;
 
rightSoftKeyAction = new QAction(tr("Open"),this);
rightSoftKeyAction->setSoftKeyRole(QAction::PositiveSoftKey);
addAction(rightSoftKeyAction);
 
connect(rightSoftKeyAction, SIGNAL(triggered()), this, SLOT(openItem()));
}
 
void MainPage::getSelectedItem(QListWidgetItem* item)
{
if(selectedRow == previousRow)
{
if(!item->text().compare("New message"))
createMessage();
else if(!item->text().left(5).compare("Inbox"))
createInbox();
else if(!item->text().left(4).compare("Sent"))
createSentItem();
else if(!item->text().left(5).compare("Draft"))
createDraft();
else if(!item->text().left(6).compare("Outbox"))
createOutbox();
}
else
{
previousRow = selectedRow;
}
}
 
 
void MainPage::getCurrentRow(int row)
{
previousRow = selectedRow;
selectedRow = row;
}
 
void MainPage::openItem()
{
getSelectedItem(listWidget->currentItem());
}
 
void MainPage::countMessagesInFolders()
{
 
/* Count SMS in inbox */
QMessageFilter messageFilter(QMessageFilter::byStandardFolder (QMessage::InboxFolder));
QMessageOrdering messageOrdering(QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder));
QMessageIdList messageIdList(QMessageStore::instance()->queryMessages(messageFilter, messageOrdering));
 
countInboxSMS = messageIdList.count();
 
/* Count SMS in sent item folder */
messageFilter = QMessageFilter::byStandardFolder (QMessage::SentFolder);
messageOrdering = QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder);
messageIdList = QMessageStore::instance()->queryMessages(messageFilter, messageOrdering);
 
countSentSMS = messageIdList.count();
 
/* Count SMS in draft folder */
messageFilter = QMessageFilter::byStandardFolder (QMessage::DraftsFolder);
messageOrdering = QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder);
messageIdList = QMessageStore::instance()->queryMessages(messageFilter, messageOrdering);
 
countDraftSMS = messageIdList.count();
 
 
/* Count SMS in outbox folder */
messageFilter = QMessageFilter::byStandardFolder (QMessage::OutboxFolder);
messageOrdering = QMessageOrdering::byReceptionTimeStamp(Qt::DescendingOrder);
messageIdList = QMessageStore::instance()->queryMessages(messageFilter, messageOrdering);
 
countOutboxSMS = messageIdList.count();
 
}
 
MainPage::~MainPage()
{
 
}



/* ListBox.cpp */
#include "ListBox.h"
#include <QListWidget>
#include <QScrollArea>
#include <QMessageBox>
#include <QAction>
 
ListBox::ListBox(QWidget *parent)
: QMainWindow(parent)
{
rightSoftKeyAction = new QAction(tr("Open"),this);
rightSoftKeyAction->setSoftKeyRole(QAction::PositiveSoftKey);
addAction(rightSoftKeyAction);
 
connect(rightSoftKeyAction, SIGNAL(triggered()), this, SLOT(openItem()));
 
leftSoftKeyAction = new QAction(tr("Back"),this);
leftSoftKeyAction->setSoftKeyRole(QAction::NegativeSoftKey);
addAction(leftSoftKeyAction);
 
connect(leftSoftKeyAction, SIGNAL(triggered()), this, SLOT(closeListBox()));
 
/* To show items in list view */
listWidget = new QListWidget();
listWidget->setObjectName(QString::fromUtf8("listWidget"));
 
/* To make UI scrollable */
scrollArea = new QScrollArea();
 
/* scrollArea takes ownership of the listWidget, so no need to delete it */
scrollArea->setWidget(listWidget);
scrollArea->setAlignment(Qt::AlignLeft);
scrollArea->setWidgetResizable(true);
 
/* QMainWindow takes ownership of the scrollArea, so no need to delete it */
setCentralWidget(scrollArea);
 
connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(getSelectedItem(QListWidgetItem*)));
connect(listWidget, SIGNAL(currentRowChanged (int)), this, SLOT(getCurrentRow(int)));
 
}
 
void ListBox::getSelectedItem(QListWidgetItem* item)
{
if(selectedRow == previousRow)
{
QMessageBox::information(0,"Text message", item->text());
}
else
{
previousRow = selectedRow;
}
}
 
void ListBox::closeListBox()
{
gotoMainPage();
}
 
void ListBox::openItem()
{
getSelectedItem(listWidget->currentItem());
}
 
void ListBox::setItemText(const QStringList& stringList)
{
listWidget->clear();
foreach ( QString str, stringList )
{
/* set font of listWiget */
QFont newFont("Courier", 6, QFont::Normal);
 
/* The listWidget will take ownership of the each QListWidgetItem item, so no need to delete it. */
QListWidgetItem* listItem = new QListWidgetItem;
listItem->setFont(newFont);
listItem->setText(str);
listItem->setSizeHint(QSize(size().width() - 200 ,70));
listWidget->addItem(listItem);
}
 
if(stringList.count() <= 0)
{
listWidget->addItem("No messages");
listWidget->setCurrentRow(0);
}
else
listWidget->setCurrentRow(0,QItemSelectionModel::Select);
 
selectedRow = previousRow = 0;
}
 
void ListBox::getCurrentRow(int row)
{
selectedRow = row;
}
 
ListBox::~ListBox()
{
}



/* SendSMS.cpp */
#include "SendSMS.h"
 
#include <QAction>
#include <QTextEdit>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QListWidget>
#include <QMessageBox>
#include <QDebug>
 
SendSMS::SendSMS(QWidget *parent)
: QWidget(parent)
{
softKeyAction = new QAction(tr("Send"),this);
softKeyAction->setSoftKeyRole(QAction::PositiveSoftKey);
addAction(softKeyAction);
 
connect(softKeyAction, SIGNAL(triggered()), this, SLOT(sendMessage()));
 
leftSoftKeyAction = new QAction(tr("Close"),this);
leftSoftKeyAction->setSoftKeyRole(QAction::NegativeSoftKey);
addAction(leftSoftKeyAction);
 
connect(leftSoftKeyAction, SIGNAL(triggered()), this, SLOT(closeEditor()));
 
/* editor to get mobile number input */
mobileNumberInput = new QLineEdit(this);
 
labelTo = new QLabel(tr("To"),this);
labelTo->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 
layout = new QGridLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(labelTo, 0, 0);
layout->setAlignment(labelTo, Qt::AlignRight);
layout->addWidget(mobileNumberInput, 0, 1);
 
/* Editor to get text message input */
messageBodyInput = new QTextEdit(this);
 
mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addLayout(layout, 0);
mainLayout->addWidget(messageBodyInput, 2);
}
 
SendSMS::~SendSMS()
{
delete layout;
}
 
void SendSMS::sendMessage()
{
//QMessageBox::warning(0, "Message Sent", "Your message is sent successfully");
 
QString to(mobileNumberInput->text());
 
/* check if receipient number is empty */
if (to.isEmpty())
{
QMessageBox::warning(0, "Missing information", "Please enter a recipient mobile number");
return;
}
 
/* check if input text is empty */
QString text(messageBodyInput->toPlainText());
if (text.isEmpty())
{
QMessageBox::warning(0, "Missing information", "Please enter a text message");
return;
}
 
mobileNumberInput->setText(tr(""));
messageBodyInput->setText(tr(""));
gotoMainPage();
 
 
QMessage message;
message.setType(QMessage::Sms);
message.setTo(QMessageAddress(to, QMessageAddress::Phone));
message.setBody(text);
 
/* send message */
messageService->send(message);
messageId = message.id();
 
}
 
void SendSMS::setMessageServiceAction(QMessageServiceAction *messService)
{
messageService = messService;
}
 
void SendSMS::closeEditor()
{
 
QString text(messageBodyInput->toPlainText());
int ret;
 
/* check if input text is empty */
if (!text.isEmpty())
{
ret = QMessageBox::question(0, "Text message", "Do you want to save message to draft??" , QMessageBox::Ok, QMessageBox::No);
}
 
if(QMessageBox::Ok == ret)
{
//save to draft
}
 
mobileNumberInput->setText(tr(""));
messageBodyInput->setText(tr(""));
 
/* go to main page */
gotoMainPage();
}

Post-conditions

The code snippet is expected to read all messages from selected folder and create & send new message.

Display all messaging folders
QtMessagesMain.jpg

Create and send new message
QtMessagesCompose.jpg

Show inbox folder
QtMessagesInBox.jpg

Read selected message from inbox
QtMessagesReadItem.jpg

Show sent item folder
QtMessagesSentItem.jpg

Download Code Example

252 page views in the last 30 days.
×