Reading contact data in Qt
Article Metadata
Tested with
Devices(s): Nokia N97
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: QContactDetailDefinition, QContactDisplayLabel, QContactDetail, QContactManager, QContact
Created: tepaa
(25 May 2010)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This code snippet demonstrates how to read contact data in Qt using the Contacts module of Qt Mobility.
Contact data can be read through QContactDetail and all of its subclasses.
Qt project file
CONFIG += mobility
MOBILITY = contacts
symbian {
TARGET.CAPABILITY = ReadUserData WriteUserData
}
Header
// QtMobility
#include <qcontactmanager.h>
#include <qcontact.h>
#include <qcontactdetailfilter.h>
#include <qcontactphonenumber.h>
#include <qcontactemailaddress.h>
QTM_USE_NAMESPACE
private:
QPointer<QContactManager> m_contactManager;
Source
1) Create manager first, see more in Finding contact manager in Qt.
// Create manager (Symbian backend)
m_contactManager = new QContactManager("symbian");
2) Search one contact, see more in Searching contact in Qt.
QContact contact = searchContact("1234567890");
3) Reading contact detail data
3.1) Data can be read using QContactDetail subclasses (for instance, QContactDisplayLabel and QContactEmailAddress).
QContactDisplayLabel displayLabel = contact.detail(QContactDisplayLabel::DefinitionName);
QMessageBox::information(this,"QContactDisplayLabel",displayLabel.label());
QContactEmailAddress email = contact.detail(QContactEmailAddress::DefinitionName);
QMessageBox::information(this,"QContactEmailAddress",email.emailAddress());
3.2) Looping through all QContactDetail of QContact
QString s;
QList<QContactDetail> allDetails = contact.details();
foreach (const QContactDetail& detail, allDetails) {
s += detail.definitionName();
s += " : ";
QVariantMap fieldValues = detail.variantValues();
QStringList keys = fieldValues.keys();
foreach (const QString& key, keys) {
s += key + "=" + detail.value(key) + ";";
}
s += "\n";
}
// Show all QContactDetail data to the user
QMessageBox::information(this,"QContactDetail",s);
Postconditions
QContactDetail data is searched and shown to the user.

