×
Namespaces

Variants
Actions

Searching contact in Qt

From Nokia Developer Wiki
Jump to: navigation, search

This code snippet demonstrates how to search contact data in Qt using the Qt Mobility Contacts module. Contact phone number is used as search criteria.

Article Metadata
Tested with
Devices(s): Nokia N97
Compatibility
Platform(s): S60 5th Edition
Maemo 5
Symbian
S60 5th Edition
Article
Keywords: QContactDetailFilter, QContactPhoneNumber, QContactManager, QContact
Created: tepaa (24 May 2010)
Last edited: hamishwillee (18 Oct 2012)

Contents

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
 
public:
QContact searchContact(const QString phoneNumber);
 
private:
QPointer<QContactManager> m_contactManager;

Source

Create manager first. For more information, see Finding contact manager in Qt.

#ifdef Q_WS_MAEMO_5
// Create manager (Maemo backend)
m_contactManager = new QContactManager("maemo5");
#elif Q_OS_SYMBIAN
// Create manager (Symbian backend)
m_contactManager = new QContactManager("symbian");
#endif

QContactManager is used for searching contact:

QContact qtSnippets::searchContact(const QString phoneNumber)
{
QContact contact;
 
// Filter for search
QContactDetailFilter phoneFilter;
phoneFilter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName,
QContactPhoneNumber::FieldNumber);
 
#ifdef Q_WS_MAEMO_5
// Workaround for Maemo bug http://bugreports.qt.nokia.com/browse/QTMOBILITY-437
phoneFilter.setMatchFlags(QContactFilter::MatchContains);
phoneFilter.setValue(phoneNumber.right(7));
#else
phoneFilter.setMatchFlags(QContactFilter::MatchPhoneNumber);
phoneFilter.setValue(phoneNumber);
#endif
 
// Search contacts
QList<QContact> matchingContacts = m_contactManager->contacts(phoneFilter);
if (matchingContacts.size() == 0) {
return contact; // return empty
}
else {
contact = matchingContacts.at(0);
return contact; // contact founds
}
}

Search contact by phone number:

QContact c = searchContact("1234567890");
if (!c.isEmpty()) {
// Show contact name to the user
QMessageBox::information(this,"Contacts found",c.displayLabel());
// Show email detail information
QContactEmailAddress email = c.detail(QContactEmailAddress::DefinitionName);
QMessageBox::information(this,"Email",email.emailAddress());
}

Postconditions

Contact is found and phone number and email address are displayed to the user.

See also

This page was last modified on 18 October 2012, at 01:10.
72 page views in the last 30 days.