Namespaces
Variants
Actions
(Difference between revisions)

Add contact to contact database using Qt Mobility

Jump to: navigation, search
m
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[Category:Qt Mobility]]
 
[[Category:Qt Mobility]]
{{CodeSnippet
+
{{ArticleMetaData
 
|id=...
 
|id=...
 
|platform=Qt
 
|platform=Qt
Line 8: Line 8:
 
|creationdate=March 24, 2010
 
|creationdate=March 24, 2010
 
|keywords= QContactManager, QContact, QLabel, QLineEdit
 
|keywords= QContactManager, QContact, QLabel, QLineEdit
 +
 +
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
 +
|author=[[User:Yashu]]
 
}}
 
}}
  
Line 21: Line 29:
  
 
==Headers required==
 
==Headers required==
<code cpp>
+
<code cpp-qt>
 
  #include <qtcontacts.h>
 
  #include <qtcontacts.h>
 
</code>
 
</code>
Line 28: Line 36:
 
Modify your .pro file as follows.
 
Modify your .pro file as follows.
  
<code cpp>
+
<code cpp-qt>
 
INCLUDEPATH += D:/QtMobility/src/contacts \
 
INCLUDEPATH += D:/QtMobility/src/contacts \
 
INCLUDEPATH += D:/QtMobility/src/contacts/filters \
 
INCLUDEPATH += D:/QtMobility/src/contacts/filters \
Line 49: Line 57:
  
 
==Header file==
 
==Header file==
<code cpp>
+
<code cpp-qt>
  
 
/* AddContact .h */
 
/* AddContact .h */
Line 84: Line 92:
  
 
==Source file==
 
==Source file==
<code cpp>
+
<code cpp-qt>
  
 
/* AddContact.cpp */
 
/* AddContact.cpp */
Line 188: Line 196:
 
* Working [[Media:QtAddContact.zip|Code Example]] is available to download from here.
 
* Working [[Media:QtAddContact.zip|Code Example]] is available to download from here.
  
  [[Category:Code Examples]]
+
  [[Category:Code Examples]][[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:23, 11 October 2012

Article Metadata

Tested with
Devices(s): Nokia 5800 XpressMusic

Compatibility
Platform(s): Qt

Article
Keywords: QContactManager, QContact, QLabel, QLineEdit
Created: Yashu (24 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)


Contents

Overview

This code snippets shows how to add contact to selected(symbian) contact manager. This code example can easily extended to add contact to any other contact manager by just iterating through available contact maangers and selecting appropriate one.

This snippet requires ReadDeviceData and WriteDeviceData capabilities. Self-signing is not possible and atleast Developer certificate is needed.

Prerequisite

Headers required

 #include <qtcontacts.h>

.pro file

Modify your .pro file as follows.

INCLUDEPATH += D:/QtMobility/src/contacts \
INCLUDEPATH += D:/QtMobility/src/contacts/filters \
INCLUDEPATH += D:/QtMobility/src/contacts/requests \
INCLUDEPATH += D:/QtMobility/src/contacts/details
INCLUDEPATH += D:/QtMobility/src/global
 
CONFIG += mobility
MOBILITY = contacts
 
symbian::TARGET.CAPABILITY = ReadUserData \
WriteUserData \
ReadDeviceData \
WriteDeviceData

UI designer(.ui) file

QtAddContactUI1.PNG

Header file

/* AddContact .h */
#ifndef ADDCONTACT_H
#define ADDCONTACT_H
 
#include <QtGui/QMainWindow>
#include "ui_AddContact.h"
#include "qtcontacts.h"
 
QTM_USE_NAMESPACE
 
class AddContact : public QMainWindow
{
 
Q_OBJECT
 
public:
AddContact(QWidget *parent = 0);
~AddContact();
 
public slots:
void resetContactDetail();
void addNewContact();
 
private:
Ui::AddContactClass ui;
QContactManager *contactManager;
};
 
#endif // ADDCONTACT_H

Source file

/* AddContact.cpp */
#include "AddContact.h"
#include <QMessageBox>
 
AddContact::AddContact(QWidget *parent)
: QMainWindow(parent), contactManager(0)
{
ui.setupUi(this);
setWindowTitle("Add contacts");
connect(ui.pushButtonAddContact, SIGNAL(clicked()), this, SLOT(addNewContact()));
connect(ui.pushButtonReset, SIGNAL(clicked()), this, SLOT(resetContactDetail()));
}
 
void AddContact::addNewContact()
{
if( (ui.inputName->text()).isEmpty() && (ui.inputLastName->text()).isEmpty() &&
(ui.inputMobile->text()).isEmpty() && (ui.inputTelephone->text()).isEmpty() &&
(ui.inputEmail->text()).isEmpty())
{
QMessageBox::information(this, "Contacts!", "Enter contact information!");
return;
}
 
if(!contactManager)
{
/* Adding contact to "symbian" contact manager */
contactManager = new QContactManager("symbian");
 
/* You can also add contact to selected contact managet by selecting
* appropriate contact manager from available contact managers.
*/

/*
QStringList availableManagers = QContactManager::availableManagers();
contactManager = new QContactManager(availableManagers.at(2));
*/

}
 
/* initialise contact details from user */
QContact curr;
QContactName name = curr.detail(QContactName::DefinitionName);
QContactPhoneNumber phone = curr.detail(QContactPhoneNumber::DefinitionName);
QContactPhoneNumber telephone = curr.detail(QContactPhoneNumber::DefinitionName);
QContactEmailAddress email = curr.detail(QContactEmailAddress::DefinitionName);
 
name.setFirst(ui.inputName->text());
name.setLast(ui.inputLastName->text());
 
phone.setContexts(QContactDetail::ContextHome);
phone.setSubTypes(QContactPhoneNumber::SubTypeMobile);
phone.setNumber(ui.inputMobile->text());
 
phone.setContexts(QContactDetail::ContextWork);
telephone.setSubTypes(QContactPhoneNumber::SubTypeLandline);
telephone.setNumber(ui.inputTelephone->text());
 
email.setEmailAddress(ui.inputEmail->text());
 
/* save details to contact object */
curr.saveDetail(&name);
curr.saveDetail(&phone);
curr.saveDetail(&telephone);
curr.saveDetail(&email);
 
/* save contact detail to contact manager */
bool success = contactManager->saveContact(&curr);
if (success)
{
QMessageBox::information(this, "Success!", "Contact " + ui.inputName->text() + " saved successfully!");
resetContactDetail();
}
else
{
QMessageBox::information(this, "Failed!", "Error in saving contact!");
}
}
 
void AddContact::resetContactDetail()
{
ui.inputName->clear();
ui.inputLastName->clear();
ui.inputMobile->clear();
ui.inputTelephone->clear();
ui.inputEmail->clear();
}
 
AddContact::~AddContact()
{
delete contactManager;
}

Post-conditions

The code snippet is expected to add contact to symbian contact manager.

QtAddContactSC1.jpg

Download Code Example

This page was last modified on 11 October 2012, at 04:23.
337 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved