Searching contact in Qt
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
Maemo 5
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
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.


(no comments yet)