(Difference between revisions)

Contact group creation and retrieval using Qt Mobility

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Add Abstract.)
hamishwillee (Talk | contribs)
m (Text replace - "Category:MeeGo" to "Category:MeeGo Harmattan")
(5 intermediate revisions by one user not shown)
Line 1: Line 1:
[[Category:PIM]][[Category:Qt Mobility]]
[[Category:PIM]][[Category:Qt Mobility]]
{{ArticleMetaData <!-- v1.2 -->
|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]]) -->
|devices= All S60 3rd/5th Edition and Symbian^3 Devices
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|devices=All S60 3rd/5th Edition and Symbian^3 Devices
|platform= Qt
|category=Qt Mobility
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|creationdate=April 20, 2011
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= QContactManager,QContact,QContactType::TypeGroup
|keywords= QContactManager,QContact,QContactType::TypeGroup
|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= 20110420
|author= [[User:Kbwiki]]
<!-- The following are not in current metadata -->
|subcategory= PIM
|id= TSQ001663
Line 180: Line 194:
[[Category:Technical Solution]]
[[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 13:52, 13 June 2012

Article Metadata
Tested with
Devices(s): All S60 3rd/5th Edition and Symbian^3 Devices
Platform(s): Qt
Keywords: QContactManager,QContact,QContactType::TypeGroup
Created: User:Kbwiki (20 Apr 2011)
Last edited: hamishwillee (13 Jun 2012)


[edit] Description

This article describes how to add a contact group to the phonebook using Qt Mobility APIs. In addition, it explains:

  1. How to add contacts to a created contact group
  2. How to retrieve contacts from a created contact group

[edit] Preconditions (optional)

Latest Qt and Qt Mobility should be available on the device.

[edit] Project file

Include the contacts from Qt Mobility:

    CONFIG += mobility
MOBILITY += contacts

Add the necessary capabilities:

    symbian: {
TARGET.CAPABILITY = ReadUserData WriteUserData

[edit] Header file

    #include <QContact>
#include <QContactManager>
#include <QObject>
#include <QString><br>
// Qt Mobility namespace declaration
class MyContactsHandler : public QObject
MyContactsHandler(QObject *parent = 0);
virtual ~MyContactsHandler();<br>
bool addGroup();
bool addContactsToGroup();
bool getContactsFromGroup(int index, QContactId &groupId, QContactId &memberId);
QString nameField(QString aContactType);<br>
private: // Data
QContactManager *m_manager;
QContact m_contactGroup;

[edit] Source file

    #include "contacthandler.h"
#include <QContactName>
#include <QContactPhoneNumber>
MyContactsHandler::MyContactsHandler(QObject *parent /* = 0 */)
 : QObject(parent),
m_manger = new QContactManager(this);
delete m_manager;
First, create the contact group.
This method is used to add a new contact group in the phone book.
bool MyContactsHandler::addGroup()
QString saveNameField = nameField(QContactType::TypeGroup);
QContactName name = m_contactGroup.detail(QContactName::DefinitionName);
name.setValue(saveNameField, "testGroupContact1");
m_contactGroup = m_manager->compatibleContact(m_contactGroup);
return m_manager->saveContact(&m_contactGroup);
After creating the contact group, add the contacts to the created group.
This method demonstrates how to add contacts into the created contact group.
bool MyContactsHandler::addContactsToGroup()
// Add one test contact to phonebook.
QContact testContact;
QString saveNameField = nameField(QContactType::TypeContact);<br>
if (!saveNameField.isEmpty()) {
QContactName name = testContact.detail(QContactName::DefinitionName);
name.setValue(saveNameField, "TestContact1");
QContactPhoneNumber phoneNumber =
testContact = m_manager->compatibleContact(testContact);
bool success = m_manager->saveContact(&testContact);<br>
// Create and save the group relationship.
QContactRelationship groupRelationship;
return m_manager->saveRelationship(&groupRelationship);
After adding the contacts to the created group, retrieve the contacts from the group.
This method demonstrates how to retrieve contacts from the created
contact group.
bool MyContactsHandler::getContactsFromGroup(int index, QContactId
&groupId, QContactId &memberId)
QList<QContactRelationship> relationshipList;
QContactRelationship relItem;
relationshipList = m_manager->relationships(m_contactGroup.id());<br>
if (index < relationshipList.count()) {
relItem = relationshipList.at(index);
groupId = relItem.first();
memberId = relItem.second();
return true;
// No contact found with the given index!
return false;
Utility method:
This method is used to get the field part for contact/contact group.
QString MyContactsHandler::nameField(QString aContactType)
QMap<QString, QContactDetailDefinition> defs =
QContactDetailDefinition nameDef =
if (nameDef.fields().keys().contains(QContactName::FieldCustomLabel)) {
return QString(QLatin1String(QContactName::FieldCustomLabel));
else if (nameDef.fields().keys().contains(QContactName::FieldFirstName)) {
return QString(QLatin1String(QContactName::FieldFirstName));
// Not found!
return QString();
This page was last modified on 13 June 2012, at 13:52.
86 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.