Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Contact group creation and retrieval using Qt Mobility

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

Contents

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

Preconditions (optional)

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

Project file

Include the contacts from Qt Mobility:

    CONFIG += mobility
MOBILITY += contacts

Add the necessary capabilities:

    symbian: {
TARGET.CAPABILITY = ReadUserData WriteUserData
}


Header file

    #include <QContact>
#include <QContactManager>
#include <QObject>
#include <QString><br>
// Qt Mobility namespace declaration
QTM_USE_NAMESPACE;<br><br>
class MyContactsHandler : public QObject
{
Q_OBJECT<br>
public:
MyContactsHandler(QObject *parent = 0);
virtual ~MyContactsHandler();<br>
public:
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;
};


Source file

    #include "contacthandler.h"
#include <QContactName>
#include <QContactPhoneNumber>
/*!
Constructor.
*/
MyContactsHandler::MyContactsHandler(QObject *parent /* = 0 */)
 : QObject(parent),
m_manager(0)
{
m_manger = new QContactManager(this);
}
/*!
Destructor.
*/
MyContactsHandler::~MyContactsHandler()
{
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()
{
m_contactGroup.setType(QContactType::TypeGroup);
QString saveNameField = nameField(QContactType::TypeGroup);
QContactName name = m_contactGroup.detail(QContactName::DefinitionName);
name.setValue(saveNameField, "testGroupContact1");
m_contactGroup.saveDetail(&name);
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");
name.setLastName("Last");
testContact.saveDetail(&name);
}<br>
QContactPhoneNumber phoneNumber =
testContact.detail(QContactPhoneNumber::DefinitionName);
phoneNumber.setNumber("9999999999");
testContact.saveDetail(&phoneNumber);
testContact = m_manager->compatibleContact(testContact);
bool success = m_manager->saveContact(&testContact);<br>
// Create and save the group relationship.
QContactRelationship groupRelationship;
groupRelationship.setFirst(m_contactGroup.id());
groupRelationship.setRelationshipType(QContactRelationship::HasMember);
groupRelationship.setSecond(testContact.id());<br>
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;
}<br>
// 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 =
m_manager->detailDefinitions(aContactType);
QContactDetailDefinition nameDef =
defs.value(QContactName::DefinitionName);<br>
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));
}<br>
// Not found!
return QString();
}
This page was last modified on 13 June 2012, at 10:52.
53 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.

×