<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/extensions/activeqt/examples/qutlook/qutlook.doc:1 --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>In Sync with Outlook</title> <style type="text/css"><!-- fn { margin-left: 1cm; text-indent: -1cm; } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } body { background: #ffffff; color: black; } --></style> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr bgcolor="#E5E5E5"> <td valign=center> <a href="index.html"> <font color="#004faf">Home</font></a> | <a href="classes.html"> <font color="#004faf">All Classes</font></a> | <a href="mainclasses.html"> <font color="#004faf">Main Classes</font></a> | <a href="annotated.html"> <font color="#004faf">Annotated</font></a> | <a href="groups.html"> <font color="#004faf">Grouped Classes</font></a> | <a href="functions.html"> <font color="#004faf">Functions</font></a> </td> <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>In Sync with Outlook</h1> This example is a modified version of the standard <a href="addressbook-example.html">TQt addressbook</a> example. <p> It demonstrates the use of <a href="qaxobject.html">TQAxObject</a> and querySubObject to instantiate and navigate the Outlook Object Model, and the use of the TQt property system to read and write values of items in the Outlook contact folder. <p> The modifications in the class declaration of the central widget are a forward declaration of the TQAxObject class and the IDispatch interface, and a new <a href="qlistviewitem.html">TQListViewItem</a> subclass <tt>ABListViewItem</tt> that implements a constructor and a destructor and has a member <tt>contact_item</tt> of type <a href="qaxobject.html">TQAxObject</a>. <pre> class TQAxObject; struct IDispatch; class ABListViewItem : public <a href="qlistviewitem.html">TQListViewItem</a> { public: ABListViewItem( <a href="ntqlistview.html">TQListView</a> *listview, TQString firstName, TQString lastName, TQString address, TQString eMail, TQAxObject *contact ); ~ABListViewItem(); <a href="qaxobject.html">TQAxObject</a> *contactItem() const; private: <a href="qaxobject.html">TQAxObject</a> *contact_item; }; </pre> <p> The ABCentralWidget gets a destructor, a new protected function <tt>setupOutlook</tt>, a new protected slot <tt>updateOutlook</tt>, and also three members of type <a href="qaxobject.html">TQAxObject</a>. <pre> void findEntries(); void updateOutlook(); protected: void setupTabWidget(); void setupListView(); void setupOutlook(); <a href="qaxobject.html">TQAxObject</a> *outlook, *outlookSession, *contactItems; <a href="qgridlayout.html">TQGridLayout</a> *mainGrid; </pre> <p> The implementation of the ABListViewItem class is trivial: <pre> ABListViewItem::ABListViewItem( <a href="ntqlistview.html">TQListView</a> *listview, <a href="ntqstring.html">TQString</a> firstName, <a href="ntqstring.html">TQString</a> lastName, <a href="ntqstring.html">TQString</a> address, <a href="ntqstring.html">TQString</a> eMail, <a href="qaxobject.html">TQAxObject</a> *contact ) : <a href="qlistviewitem.html">TQListViewItem</a>( listview, firstName, lastName, address, eMail ), contact_item( contact ) { } ABListViewItem::~ABListViewItem() { delete contact_item; } TQAxObject *ABListViewItem::contactItem() const { return contact_item; } </pre>The ABCentralWidget constructor initializes the <a href="qaxobject.html">TQAxObject</a> pointers to zero and calls the <tt>setupOutlook</tt> function. The ABCentralWidget destructor calls the Logoff method of the outlookSession object. <pre> ABCentralWidget::ABCentralWidget( <a href="ntqwidget.html">TQWidget</a> *parent, const char *name ) : <a href="ntqwidget.html">TQWidget</a>( parent, name ), outlook( 0 ), outlookSession( 0 ), contactItems( 0 ) { mainGrid = new <a href="qgridlayout.html">TQGridLayout</a>( this, 2, 1, 5, 5 ); setupTabWidget(); setupListView(); setupOutlook(); <a name="x2722"></a> mainGrid-><a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 0, 0 ); mainGrid-><a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 1, 1 ); } ABCentralWidget::~ABCentralWidget() { if ( outlookSession ) outlookSession->dynamicCall( "Logoff()" ); } </pre>The <tt>setupOutlook</tt> implementation creates a TQAxObject to wrap the Outlook.Application COM object. <pre> void ABCentralWidget::setupOutlook() { outlook = new <a href="qaxobject.html">TQAxObject</a>( "Outlook.Application", this ); </pre>The call to <tt>querySubObject</tt> returns a new <a href="qaxobject.html">TQAxObject</a> wrapper around the "Session" object of the Outlook Object hierarchy. If the call fails for some reason setupOutlook returns, otherwise it calls the "Logon" method of the Session object. <pre> // Get a session object <a name="x2721"></a> outlookSession = outlook-><a href="qaxbase.html#querySubObject">querySubObject</a>( "Session" ); if ( !outlookSession ) return; // Login; doesn't hurt if you are already running and logged on... outlookSession->dynamicCall( "Logon()" ); </pre>The following call to <tt>querySubObject</tt> returns a new TQAxObject wrapper around the default folder for "contacts". <pre> // Get the default folder for contacts <a href="qaxobject.html">TQAxObject</a> *defFolder = outlookSession->querySubObject( "GetDefaultFolder(OlDefaultFolders)", "olFolderContacts" ); </pre><tt>querySubObject</tt> is then used again to get the list of all items in the folder. The <tt>connect</tt> statement connects the new ABCentralWidget slot to the signals provided by the "Items" COM object. Finally, it calls the <tt>updateOutlook</tt> function to populate the listview. <pre> // Get all items if ( defFolder ) { contactItems = defFolder-><a href="qaxbase.html#querySubObject">querySubObject</a>( "Items" ); <a href="ntqobject.html#connect">connect</a>( contactItems, SIGNAL(ItemAdd(IDispatch*)), this, SLOT(updateOutlook()) ); <a href="ntqobject.html#connect">connect</a>( contactItems, SIGNAL(ItemChange(IDispatch*)), this, SLOT(updateOutlook()) ); <a href="ntqobject.html#connect">connect</a>( contactItems, SIGNAL(ItemRemove()), this, SLOT(updateOutlook()) ); } updateOutlook(); } </pre> <p> The implementation of the <tt>updateOutlook</tt> slot clears the listview, and uses <tt>querySubObject</tt> to iterate through the list of items. For every item provided a new ABListViewItem object is created and filled with the properties of the item object. The object returned by <tt>querySubObject</tt> is a child of the callee (ie. "contactItems"), but the list view item should take ownership to provide a cleaner relation between entries, so the item has to be removed from its parent object. <pre> void ABCentralWidget::updateOutlook() { <a name="x2725"></a> listView-><a href="ntqlistview.html#clear">clear</a>(); if ( !contactItems ) return; <a href="qaxobject.html">TQAxObject</a> *item = contactItems->querySubObject( "GetFirst()" ); while ( item ) { <a name="x2729"></a> <a href="ntqstring.html">TQString</a> firstName = item-><a href="ntqobject.html#property">property</a>( "FirstName" ).toString(); <a href="ntqstring.html">TQString</a> lastName = item-><a href="ntqobject.html#property">property</a>( "LastName" ).toString(); <a href="ntqstring.html">TQString</a> address = item-><a href="ntqobject.html#property">property</a>( "HomeAddress" ).toString(); <a href="ntqstring.html">TQString</a> email = item-><a href="ntqobject.html#property">property</a>( "Email1Address" ).toString(); (void)new ABListViewItem( listView, firstName, lastName, address, email, item ); // the listviewitem takes ownership <a name="x2727"></a> item-><a href="qlistviewitem.html#parent">parent</a>()->removeChild( item ); item = contactItems->querySubObject( "GetNext()" ); } } </pre> <p> The <tt>addEntry</tt> implementation calls the CreateItem method of the Outlook.Application object to create a new contact item, and creates a new ABListViewItem if the call succeeds. <pre> void ABCentralWidget::addEntry() { <a name="x2724"></a> if ( !iFirstName-><a href="ntqlineedit.html#text">text</a>().isEmpty() || !iLastName-><a href="ntqlineedit.html#text">text</a>().isEmpty() || !iAddress-><a href="ntqlineedit.html#text">text</a>().isEmpty() || !iEMail-><a href="ntqlineedit.html#text">text</a>().isEmpty() ) { <a href="qaxobject.html">TQAxObject</a> *contactItem = outlook-><a href="qaxbase.html#querySubObject">querySubObject</a>( "CreateItem(OlItemType)", "olContactItem" ); if ( contactItem ) { <a name="x2730"></a> contactItem-><a href="ntqobject.html#setProperty">setProperty</a>( "FirstName", iFirstName-><a href="ntqlineedit.html#text">text</a>() ); contactItem-><a href="ntqobject.html#setProperty">setProperty</a>( "LastName", iLastName-><a href="ntqlineedit.html#text">text</a>() ); contactItem-><a href="ntqobject.html#setProperty">setProperty</a>( "HomeAddress", iAddress-><a href="ntqlineedit.html#text">text</a>() ); contactItem-><a href="ntqobject.html#setProperty">setProperty</a>( "Email1Address", iEMail-><a href="ntqlineedit.html#text">text</a>() ); <a name="x2720"></a> contactItem-><a href="qaxbase.html#dynamicCall">dynamicCall</a>( "Save()" ); new ABListViewItem( listView, iFirstName-><a href="ntqlineedit.html#text">text</a>(), iLastName-><a href="ntqlineedit.html#text">text</a>(), iAddress-><a href="ntqlineedit.html#text">text</a>(), iEMail-><a href="ntqlineedit.html#text">text</a>(), contactItem ); } } <a name="x2723"></a> iFirstName-><a href="ntqlineedit.html#setText">setText</a>( "" ); iLastName-><a href="ntqlineedit.html#setText">setText</a>( "" ); iAddress-><a href="ntqlineedit.html#setText">setText</a>( "" ); iEMail-><a href="ntqlineedit.html#setText">setText</a>( "" ); } </pre> <p> The <tt>changeEntry</tt> implementation updates the values in the contact item of the current listview item as well as the values of the listview item itself. <pre> void ABCentralWidget::changeEntry() { <a name="x2726"></a> ABListViewItem *item = (ABListViewItem*)listView-><a href="ntqlistview.html#currentItem">currentItem</a>(); if ( item && ( !iFirstName-><a href="ntqlineedit.html#text">text</a>().isEmpty() || !iLastName-><a href="ntqlineedit.html#text">text</a>().isEmpty() || !iAddress-><a href="ntqlineedit.html#text">text</a>().isEmpty() || !iEMail-><a href="ntqlineedit.html#text">text</a>().isEmpty() ) ) { <a href="qaxobject.html">TQAxObject</a> *contactItem = item->contactItem(); contactItem-><a href="ntqobject.html#setProperty">setProperty</a>( "FirstName", iFirstName-><a href="ntqlineedit.html#text">text</a>() ); contactItem-><a href="ntqobject.html#setProperty">setProperty</a>( "LastName", iLastName-><a href="ntqlineedit.html#text">text</a>() ); contactItem-><a href="ntqobject.html#setProperty">setProperty</a>( "HomeAddress", iAddress-><a href="ntqlineedit.html#text">text</a>() ); contactItem-><a href="ntqobject.html#setProperty">setProperty</a>( "Email1Address", iEMail-><a href="ntqlineedit.html#text">text</a>() ); contactItem-><a href="qaxbase.html#dynamicCall">dynamicCall</a>( "Save()" ); <a name="x2728"></a> item-><a href="qlistviewitem.html#setText">setText</a>( 0, iFirstName-><a href="ntqlineedit.html#text">text</a>() ); item-><a href="qlistviewitem.html#setText">setText</a>( 1, iLastName-><a href="ntqlineedit.html#text">text</a>() ); item-><a href="qlistviewitem.html#setText">setText</a>( 2, iAddress-><a href="ntqlineedit.html#text">text</a>() ); item-><a href="qlistviewitem.html#setText">setText</a>( 3, iEMail-><a href="ntqlineedit.html#text">text</a>() ); } } </pre> <p> To build the example you must first build the <a href="qaxcontainer.html">TQAxContainer</a> library. Then run your make tool in <tt>examples/qutlook</tt> and run the resulting <tt>qutlok.exe</tt>. <p>See also <a href="qaxcontainer-examples.html">The TQAxContainer Examples</a>. <!-- eof --> <p><address><hr><div align=center> <table width=100% cellspacing=0 border=0><tr> <td>Copyright © 2007 <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> <td align=right><div align=right>TQt 3.3.8</div> </table></div></address></body> </html>