[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/extensions/activeqt/examples/qutlook/qutlook.doc:1 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>In Sync with Outlook</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <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>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | This example is a modified version of the standard
|
---|
| 37 | <a href="addressbook-example.html">Qt addressbook</a> example.
|
---|
| 38 | <p> It demonstrates the use of <a href="qaxobject.html">QAxObject</a> and querySubObject to instantiate and
|
---|
| 39 | navigate the Outlook Object Model, and the use of the Qt property system to
|
---|
| 40 | read and write values of items in the Outlook contact folder.
|
---|
| 41 | <p>
|
---|
| 42 |
|
---|
| 43 | The modifications in the class declaration of the central widget are
|
---|
| 44 | a forward declaration of the QAxObject class and the IDispatch interface,
|
---|
| 45 | and a new <a href="qlistviewitem.html">QListViewItem</a> subclass <tt>ABListViewItem</tt> that implements a
|
---|
| 46 | constructor and a destructor and has a member <tt>contact_item</tt> of type
|
---|
| 47 | <a href="qaxobject.html">QAxObject</a>.
|
---|
| 48 | <pre> class QAxObject;
|
---|
| 49 | struct IDispatch;
|
---|
| 50 |
|
---|
| 51 | class ABListViewItem : public <a href="qlistviewitem.html">QListViewItem</a>
|
---|
| 52 | {
|
---|
| 53 | public:
|
---|
| 54 | ABListViewItem( <a href="qlistview.html">QListView</a> *listview, QString firstName, QString lastName, QString address, QString eMail, QAxObject *contact );
|
---|
| 55 | ~ABListViewItem();
|
---|
| 56 |
|
---|
| 57 | <a href="qaxobject.html">QAxObject</a> *contactItem() const;
|
---|
| 58 |
|
---|
| 59 | private:
|
---|
| 60 | <a href="qaxobject.html">QAxObject</a> *contact_item;
|
---|
| 61 | };
|
---|
| 62 | </pre>
|
---|
| 63 | <p> The ABCentralWidget gets a destructor, a new protected function <tt>setupOutlook</tt>,
|
---|
| 64 | a new protected slot <tt>updateOutlook</tt>, and also three members of type <a href="qaxobject.html">QAxObject</a>.
|
---|
| 65 | <pre> void findEntries();
|
---|
| 66 |
|
---|
| 67 | void updateOutlook();
|
---|
| 68 |
|
---|
| 69 | protected:
|
---|
| 70 | void setupTabWidget();
|
---|
| 71 | void setupListView();
|
---|
| 72 | void setupOutlook();
|
---|
| 73 |
|
---|
| 74 | <a href="qaxobject.html">QAxObject</a> *outlook, *outlookSession, *contactItems;
|
---|
| 75 |
|
---|
| 76 | <a href="qgridlayout.html">QGridLayout</a> *mainGrid;
|
---|
| 77 | </pre>
|
---|
| 78 | <p>
|
---|
| 79 |
|
---|
| 80 | The implementation of the ABListViewItem class is trivial:
|
---|
| 81 | <pre> ABListViewItem::ABListViewItem( <a href="qlistview.html">QListView</a> *listview,
|
---|
| 82 | <a href="qstring.html">QString</a> firstName,
|
---|
| 83 | <a href="qstring.html">QString</a> lastName,
|
---|
| 84 | <a href="qstring.html">QString</a> address,
|
---|
| 85 | <a href="qstring.html">QString</a> eMail,
|
---|
| 86 | <a href="qaxobject.html">QAxObject</a> *contact )
|
---|
| 87 | : <a href="qlistviewitem.html">QListViewItem</a>( listview, firstName, lastName, address, eMail ), contact_item( contact )
|
---|
| 88 | {
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | ABListViewItem::~ABListViewItem()
|
---|
| 92 | {
|
---|
| 93 | delete contact_item;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | QAxObject *ABListViewItem::contactItem() const
|
---|
| 97 | {
|
---|
| 98 | return contact_item;
|
---|
| 99 | }
|
---|
| 100 | </pre>The ABCentralWidget constructor initializes the <a href="qaxobject.html">QAxObject</a> pointers to zero and
|
---|
| 101 | calls the <tt>setupOutlook</tt> function. The ABCentralWidget destructor calls the
|
---|
| 102 | Logoff method of the outlookSession object.
|
---|
| 103 | <pre> ABCentralWidget::ABCentralWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 104 | : <a href="qwidget.html">QWidget</a>( parent, name ), outlook( 0 ), outlookSession( 0 ), contactItems( 0 )
|
---|
| 105 | {
|
---|
| 106 | mainGrid = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 1, 5, 5 );
|
---|
| 107 |
|
---|
| 108 | setupTabWidget();
|
---|
| 109 | setupListView();
|
---|
| 110 | setupOutlook();
|
---|
| 111 |
|
---|
| 112 | <a name="x2722"></a> mainGrid-><a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 0, 0 );
|
---|
| 113 | mainGrid-><a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 1, 1 );
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | ABCentralWidget::~ABCentralWidget()
|
---|
| 117 | {
|
---|
| 118 | if ( outlookSession )
|
---|
| 119 | outlookSession->dynamicCall( "Logoff()" );
|
---|
| 120 | }
|
---|
| 121 | </pre>The <tt>setupOutlook</tt> implementation creates a QAxObject to wrap the
|
---|
| 122 | Outlook.Application COM object.
|
---|
| 123 | <pre> void ABCentralWidget::setupOutlook()
|
---|
| 124 | {
|
---|
| 125 | outlook = new <a href="qaxobject.html">QAxObject</a>( "Outlook.Application", this );
|
---|
| 126 | </pre>The call to <tt>querySubObject</tt> returns a new <a href="qaxobject.html">QAxObject</a> wrapper around the
|
---|
| 127 | "Session" object of the Outlook Object hierarchy. If the call fails for
|
---|
| 128 | some reason setupOutlook returns, otherwise it calls the "Logon" method
|
---|
| 129 | of the Session object.
|
---|
| 130 | <pre> // Get a session object
|
---|
| 131 | <a name="x2721"></a> outlookSession = outlook-><a href="qaxbase.html#querySubObject">querySubObject</a>( "Session" );
|
---|
| 132 | if ( !outlookSession )
|
---|
| 133 | return;
|
---|
| 134 | // Login; doesn't hurt if you are already running and logged on...
|
---|
| 135 | outlookSession->dynamicCall( "Logon()" );
|
---|
| 136 | </pre>The following call to <tt>querySubObject</tt> returns a new QAxObject wrapper
|
---|
| 137 | around the default folder for "contacts".
|
---|
| 138 | <pre> // Get the default folder for contacts
|
---|
| 139 | <a href="qaxobject.html">QAxObject</a> *defFolder = outlookSession->querySubObject( "GetDefaultFolder(OlDefaultFolders)", "olFolderContacts" );
|
---|
| 140 | </pre><tt>querySubObject</tt> is then used again to get the list of all items in the
|
---|
| 141 | folder. The <tt>connect</tt> statement connects the new ABCentralWidget slot
|
---|
| 142 | to the signals provided by the "Items" COM object. Finally, it calls the
|
---|
| 143 | <tt>updateOutlook</tt> function to populate the listview.
|
---|
| 144 | <pre> // Get all items
|
---|
| 145 | if ( defFolder ) {
|
---|
| 146 | contactItems = defFolder-><a href="qaxbase.html#querySubObject">querySubObject</a>( "Items" );
|
---|
| 147 | <a href="qobject.html#connect">connect</a>( contactItems, SIGNAL(ItemAdd(IDispatch*)), this, SLOT(updateOutlook()) );
|
---|
| 148 | <a href="qobject.html#connect">connect</a>( contactItems, SIGNAL(ItemChange(IDispatch*)), this, SLOT(updateOutlook()) );
|
---|
| 149 | <a href="qobject.html#connect">connect</a>( contactItems, SIGNAL(ItemRemove()), this, SLOT(updateOutlook()) );
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | updateOutlook();
|
---|
| 153 | }
|
---|
| 154 | </pre>
|
---|
| 155 | <p> The implementation of the <tt>updateOutlook</tt> slot clears the listview, and uses
|
---|
| 156 | <tt>querySubObject</tt> to iterate through the list of items. For every item provided a new
|
---|
| 157 | ABListViewItem object is created and filled with the properties of the item
|
---|
| 158 | object. The object returned by <tt>querySubObject</tt> is a child of the callee (ie. "contactItems"),
|
---|
| 159 | but the list view item should take ownership to provide a cleaner relation between
|
---|
| 160 | entries, so the item has to be removed from its parent object.
|
---|
| 161 | <pre> void ABCentralWidget::updateOutlook()
|
---|
| 162 | {
|
---|
| 163 | <a name="x2725"></a> listView-><a href="qlistview.html#clear">clear</a>();
|
---|
| 164 | if ( !contactItems )
|
---|
| 165 | return;
|
---|
| 166 |
|
---|
| 167 | <a href="qaxobject.html">QAxObject</a> *item = contactItems->querySubObject( "GetFirst()" );
|
---|
| 168 | while ( item ) {
|
---|
| 169 | <a name="x2729"></a> <a href="qstring.html">QString</a> firstName = item-><a href="qobject.html#property">property</a>( "FirstName" ).toString();
|
---|
| 170 | <a href="qstring.html">QString</a> lastName = item-><a href="qobject.html#property">property</a>( "LastName" ).toString();
|
---|
| 171 | <a href="qstring.html">QString</a> address = item-><a href="qobject.html#property">property</a>( "HomeAddress" ).toString();
|
---|
| 172 | <a href="qstring.html">QString</a> email = item-><a href="qobject.html#property">property</a>( "Email1Address" ).toString();
|
---|
| 173 |
|
---|
| 174 | (void)new ABListViewItem( listView, firstName, lastName, address, email, item );
|
---|
| 175 | // the listviewitem takes ownership
|
---|
| 176 | <a name="x2727"></a> item-><a href="qlistviewitem.html#parent">parent</a>()->removeChild( item );
|
---|
| 177 |
|
---|
| 178 | item = contactItems->querySubObject( "GetNext()" );
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | </pre>
|
---|
| 182 | <p> The <tt>addEntry</tt> implementation calls the CreateItem method of the Outlook.Application
|
---|
| 183 | object to create a new contact item, and creates a new ABListViewItem if the call
|
---|
| 184 | succeeds.
|
---|
| 185 | <pre> void ABCentralWidget::addEntry()
|
---|
| 186 | {
|
---|
| 187 | <a name="x2724"></a> if ( !iFirstName-><a href="qlineedit.html#text">text</a>().isEmpty() || !iLastName-><a href="qlineedit.html#text">text</a>().isEmpty() ||
|
---|
| 188 | !iAddress-><a href="qlineedit.html#text">text</a>().isEmpty() || !iEMail-><a href="qlineedit.html#text">text</a>().isEmpty() ) {
|
---|
| 189 | <a href="qaxobject.html">QAxObject</a> *contactItem = outlook-><a href="qaxbase.html#querySubObject">querySubObject</a>( "CreateItem(OlItemType)", "olContactItem" );
|
---|
| 190 | if ( contactItem ) {
|
---|
| 191 | <a name="x2730"></a> contactItem-><a href="qobject.html#setProperty">setProperty</a>( "FirstName", iFirstName-><a href="qlineedit.html#text">text</a>() );
|
---|
| 192 | contactItem-><a href="qobject.html#setProperty">setProperty</a>( "LastName", iLastName-><a href="qlineedit.html#text">text</a>() );
|
---|
| 193 | contactItem-><a href="qobject.html#setProperty">setProperty</a>( "HomeAddress", iAddress-><a href="qlineedit.html#text">text</a>() );
|
---|
| 194 | contactItem-><a href="qobject.html#setProperty">setProperty</a>( "Email1Address", iEMail-><a href="qlineedit.html#text">text</a>() );
|
---|
| 195 | <a name="x2720"></a> contactItem-><a href="qaxbase.html#dynamicCall">dynamicCall</a>( "Save()" );
|
---|
| 196 |
|
---|
| 197 | new ABListViewItem( listView, iFirstName-><a href="qlineedit.html#text">text</a>(),
|
---|
| 198 | iLastName-><a href="qlineedit.html#text">text</a>(), iAddress-><a href="qlineedit.html#text">text</a>(), iEMail-><a href="qlineedit.html#text">text</a>(), contactItem );
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | <a name="x2723"></a> iFirstName-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
| 203 | iLastName-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
| 204 | iAddress-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
| 205 | iEMail-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
| 206 | }
|
---|
| 207 | </pre>
|
---|
| 208 | <p> The <tt>changeEntry</tt> implementation updates the values in the contact item of the current
|
---|
| 209 | listview item as well as the values of the listview item itself.
|
---|
| 210 | <pre> void ABCentralWidget::changeEntry()
|
---|
| 211 | {
|
---|
| 212 | <a name="x2726"></a> ABListViewItem *item = (ABListViewItem*)listView-><a href="qlistview.html#currentItem">currentItem</a>();
|
---|
| 213 |
|
---|
| 214 | if ( item &&
|
---|
| 215 | ( !iFirstName-><a href="qlineedit.html#text">text</a>().isEmpty() || !iLastName-><a href="qlineedit.html#text">text</a>().isEmpty() ||
|
---|
| 216 | !iAddress-><a href="qlineedit.html#text">text</a>().isEmpty() || !iEMail-><a href="qlineedit.html#text">text</a>().isEmpty() ) ) {
|
---|
| 217 |
|
---|
| 218 | <a href="qaxobject.html">QAxObject</a> *contactItem = item->contactItem();
|
---|
| 219 | contactItem-><a href="qobject.html#setProperty">setProperty</a>( "FirstName", iFirstName-><a href="qlineedit.html#text">text</a>() );
|
---|
| 220 | contactItem-><a href="qobject.html#setProperty">setProperty</a>( "LastName", iLastName-><a href="qlineedit.html#text">text</a>() );
|
---|
| 221 | contactItem-><a href="qobject.html#setProperty">setProperty</a>( "HomeAddress", iAddress-><a href="qlineedit.html#text">text</a>() );
|
---|
| 222 | contactItem-><a href="qobject.html#setProperty">setProperty</a>( "Email1Address", iEMail-><a href="qlineedit.html#text">text</a>() );
|
---|
| 223 | contactItem-><a href="qaxbase.html#dynamicCall">dynamicCall</a>( "Save()" );
|
---|
| 224 |
|
---|
| 225 | <a name="x2728"></a> item-><a href="qlistviewitem.html#setText">setText</a>( 0, iFirstName-><a href="qlineedit.html#text">text</a>() );
|
---|
| 226 | item-><a href="qlistviewitem.html#setText">setText</a>( 1, iLastName-><a href="qlineedit.html#text">text</a>() );
|
---|
| 227 | item-><a href="qlistviewitem.html#setText">setText</a>( 2, iAddress-><a href="qlineedit.html#text">text</a>() );
|
---|
| 228 | item-><a href="qlistviewitem.html#setText">setText</a>( 3, iEMail-><a href="qlineedit.html#text">text</a>() );
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | </pre>
|
---|
| 232 | <p> To build the example you must first build the <a href="qaxcontainer.html">QAxContainer</a>
|
---|
| 233 | library. Then run your make tool in <tt>examples/qutlook</tt> and run the resulting <tt>qutlok.exe</tt>.
|
---|
| 234 | <p>See also <a href="qaxcontainer-examples.html">The QAxContainer Examples</a>.
|
---|
| 235 |
|
---|
| 236 | <!-- eof -->
|
---|
| 237 | <p><address><hr><div align=center>
|
---|
| 238 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 239 | <td>Copyright © 2007
|
---|
| 240 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 241 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 242 | </table></div></address></body>
|
---|
| 243 | </html>
|
---|