×
Namespaces

Variants
Actions
(Difference between revisions)

Add contact to contact database using Qt Mobility

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Addition to article of: Category:MeeGo Category:Symbian. (Add platform categories))
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(One intermediate revision by one user not shown)
Line 29: Line 29:
  
 
==Headers required==
 
==Headers required==
<code cpp>
+
<code cpp-qt>
 
  #include <qtcontacts.h>
 
  #include <qtcontacts.h>
 
</code>
 
</code>
Line 36: 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 57: Line 57:
  
 
==Header file==
 
==Header file==
<code cpp>
+
<code cpp-qt>
  
 
/* AddContact .h */
 
/* AddContact .h */
Line 92: Line 92:
  
 
==Source file==
 
==Source file==
<code cpp>
+
<code cpp-qt>
  
 
/* AddContact.cpp */
 
/* AddContact.cpp */
Line 196: 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:MeeGo]] [[Category:Symbian]]
+
  [[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
Symbian
Article
Keywords: QContactManager, QContact, QLabel, QLineEdit
Created: Yashu (24 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)


Contents

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

[edit] Prerequisite

[edit] Headers required

 #include <qtcontacts.h>

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

[edit] UI designer(.ui) file

QtAddContactUI1.PNG

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

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

[edit] Post-conditions

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

QtAddContactSC1.jpg

[edit] Download Code Example

This page was last modified on 11 October 2012, at 04:23.
90 page views in the last 30 days.
×