Finding contact manager in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:Abstract

Template:ArticleMetaData

Qt project file[edit | edit source]

CONFIG += mobility MOBILITY = contacts

symbian { TARGET.CAPABILITY = ReadUserData WriteUserData }

Header[edit | edit source]

  1. include <QtGui/QMainWindow>
  2. include <QPointer>

// QtMobility

  1. include <qcontactmanager.h>
  2. include <qcontact.h>

QTM_USE_NAMESPACE

class qtSnippets : public QMainWindow {

   Q_OBJECT

public:

   qtSnippets(QWidget *parent = 0);
   ~qtSnippets();

private slots:

   void createManager();

private:

   QPointer<QContactManager> m_contactManager;

};

Source[edit | edit source]

  1. include <QTimer>
  2. include <QMessageBox>

qtSnippets::qtSnippets(QWidget *parent)

   : QMainWindow(parent)

{

   // Let application to startup fully and then
   // create manager
   QTimer::singleShot(0, this, SLOT(createManager()));

}

qtSnippets::~qtSnippets() {

   delete m_contactManager;

}

void qtSnippets::createManager() {

   // Get list of different contact back-ends
   QStringList availableManagers = QContactManager::availableManagers();
   QList<QContactLocalId> contactIds;
   // Try to find contacts from some back-end
   while (!availableManagers.isEmpty()) {
       // Get some manager
       m_contactManager = new QContactManager(availableManagers.first());
       availableManagers.removeFirst();
       // Contacts exists?
       contactIds = m_contactManager->contactIds();
       if (!contactIds.isEmpty()) {
           // Contact found
           availableManagers.clear();
           break;
       }
       else {
           // Not found, try the next manager
           delete m_contactManager;
           m_contactManager = 0;
       }
   }
   // Use default if no contact found from any back-end
   if (!m_contactManager) {
       m_contactManager = new QContactManager();
   }
   // Show message to the user
   QString msg = QString("Manager %1 created, that has %2 contacts")
   .arg(m_contactManager->managerName()).arg(contactIds.count());
   QMessageBox::information(this,"Contacts",msg);

}

You can also create QContactManager to stack using symbian or maemo5 backend keys: // Create manager (Symbian backend) QContactManager contactManager("symbian");

// Create manager (Maemo 5, Nokia N900 backend) QContactManager contactManager("maemo5");


Postconditions[edit | edit source]

Contact manager is found.

See also[edit | edit source]