×
Namespaces

Variants
Actions
(Difference between revisions)

Contact group creation and retrieval using Qt Mobility

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
hamishwillee (Talk | contribs)
m (Add Abstract.)
Line 1: Line 1:
[[Category:PIM]][[Category:Qt]]
+
[[Category:PIM]][[Category:Qt Mobility]]
 
{{KBTS}}
 
{{KBTS}}
 
{{KnowledgeBase
 
{{KnowledgeBase
Line 6: Line 6:
 
|platform=Qt
 
|platform=Qt
 
|devices=All S60 3rd/5th Edition and Symbian^3 Devices
 
|devices=All S60 3rd/5th Edition and Symbian^3 Devices
|category=PIM
+
|category=Qt Mobility
|subcategory=Phonebook
+
|subcategory=PIM
 
|creationdate=April 20, 2011
 
|creationdate=April 20, 2011
 
|keywords= QContactManager,QContact,QContactType::TypeGroup
 
|keywords= QContactManager,QContact,QContactType::TypeGroup
Line 14: Line 14:
 
==Description==
 
==Description==
  
This article describes how to add a contact group to the phonebook using Qt Mobility APIs. In addition, this will explain:
+
{{Abstract|This article describes how to add a contact group to the phonebook using Qt Mobility APIs.}} In addition, it explains:
 
+
# How to add contacts to a created contact group
<br>1. How to add contacts to a created contact group
+
# How to retrieve contacts from a created contact group
<br>2. How to retrieve contacts from a created contact group
+
  
 
==Preconditions (optional)==
 
==Preconditions (optional)==
Line 181: Line 180:
  
  
[[Category:Technical Solution]][[Category:Phonebook]]
+
[[Category:Technical Solution]]

Revision as of 04:17, 6 May 2011

Template:KBTS

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: (20 Apr 2011)
Last edited: hamishwillee (06 May 2011)

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();
}
88 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.

×