source: trunk/doc/html/qaxcontainer-example-qutlook.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 13.5 KB
Line 
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"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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
36This 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
39navigate the Outlook Object Model, and the use of the Qt property system to
40read and write values of items in the Outlook contact folder.
41<p>
42
43The modifications in the class declaration of the central widget are
44a forward declaration of the QAxObject class and the IDispatch interface,
45and a new <a href="qlistviewitem.html">QListViewItem</a> subclass <tt>ABListViewItem</tt> that implements a
46constructor 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>,
64a 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
80The 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
101calls the <tt>setupOutlook</tt> function. The ABCentralWidget destructor calls the
102Logoff 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-&gt;<a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 0, 0 );
113 mainGrid-&gt;<a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 1, 1 );
114 }
115
116 ABCentralWidget::~ABCentralWidget()
117 {
118 if ( outlookSession )
119 outlookSession-&gt;dynamicCall( "Logoff()" );
120 }
121</pre>The <tt>setupOutlook</tt> implementation creates a QAxObject to wrap the
122Outlook.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
128some reason setupOutlook returns, otherwise it calls the "Logon" method
129of the Session object.
130<pre> // Get a session object
131 <a name="x2721"></a> outlookSession = outlook-&gt;<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-&gt;dynamicCall( "Logon()" );
136</pre>The following call to <tt>querySubObject</tt> returns a new QAxObject wrapper
137around the default folder for "contacts".
138<pre> // Get the default folder for contacts
139 <a href="qaxobject.html">QAxObject</a> *defFolder = outlookSession-&gt;querySubObject( "GetDefaultFolder(OlDefaultFolders)", "olFolderContacts" );
140</pre><tt>querySubObject</tt> is then used again to get the list of all items in the
141folder. The <tt>connect</tt> statement connects the new ABCentralWidget slot
142to 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-&gt;<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
157ABListViewItem object is created and filled with the properties of the item
158object. The object returned by <tt>querySubObject</tt> is a child of the callee (ie. "contactItems"),
159but the list view item should take ownership to provide a cleaner relation between
160entries, so the item has to be removed from its parent object.
161<pre> void ABCentralWidget::updateOutlook()
162 {
163 <a name="x2725"></a> listView-&gt;<a href="qlistview.html#clear">clear</a>();
164 if ( !contactItems )
165 return;
166
167 <a href="qaxobject.html">QAxObject</a> *item = contactItems-&gt;querySubObject( "GetFirst()" );
168 while ( item ) {
169 <a name="x2729"></a> <a href="qstring.html">QString</a> firstName = item-&gt;<a href="qobject.html#property">property</a>( "FirstName" ).toString();
170 <a href="qstring.html">QString</a> lastName = item-&gt;<a href="qobject.html#property">property</a>( "LastName" ).toString();
171 <a href="qstring.html">QString</a> address = item-&gt;<a href="qobject.html#property">property</a>( "HomeAddress" ).toString();
172 <a href="qstring.html">QString</a> email = item-&gt;<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-&gt;<a href="qlistviewitem.html#parent">parent</a>()-&gt;removeChild( item );
177
178 item = contactItems-&gt;querySubObject( "GetNext()" );
179 }
180 }
181</pre>
182<p> The <tt>addEntry</tt> implementation calls the CreateItem method of the Outlook.Application
183object to create a new contact item, and creates a new ABListViewItem if the call
184succeeds.
185<pre> void ABCentralWidget::addEntry()
186 {
187 <a name="x2724"></a> if ( !iFirstName-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() || !iLastName-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() ||
188 !iAddress-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() || !iEMail-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() ) {
189 <a href="qaxobject.html">QAxObject</a> *contactItem = outlook-&gt;<a href="qaxbase.html#querySubObject">querySubObject</a>( "CreateItem(OlItemType)", "olContactItem" );
190 if ( contactItem ) {
191 <a name="x2730"></a> contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "FirstName", iFirstName-&gt;<a href="qlineedit.html#text">text</a>() );
192 contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "LastName", iLastName-&gt;<a href="qlineedit.html#text">text</a>() );
193 contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "HomeAddress", iAddress-&gt;<a href="qlineedit.html#text">text</a>() );
194 contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "Email1Address", iEMail-&gt;<a href="qlineedit.html#text">text</a>() );
195 <a name="x2720"></a> contactItem-&gt;<a href="qaxbase.html#dynamicCall">dynamicCall</a>( "Save()" );
196
197 new ABListViewItem( listView, iFirstName-&gt;<a href="qlineedit.html#text">text</a>(),
198 iLastName-&gt;<a href="qlineedit.html#text">text</a>(), iAddress-&gt;<a href="qlineedit.html#text">text</a>(), iEMail-&gt;<a href="qlineedit.html#text">text</a>(), contactItem );
199 }
200 }
201
202 <a name="x2723"></a> iFirstName-&gt;<a href="qlineedit.html#setText">setText</a>( "" );
203 iLastName-&gt;<a href="qlineedit.html#setText">setText</a>( "" );
204 iAddress-&gt;<a href="qlineedit.html#setText">setText</a>( "" );
205 iEMail-&gt;<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
209listview 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-&gt;<a href="qlistview.html#currentItem">currentItem</a>();
213
214 if ( item &amp;&amp;
215 ( !iFirstName-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() || !iLastName-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() ||
216 !iAddress-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() || !iEMail-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() ) ) {
217
218 <a href="qaxobject.html">QAxObject</a> *contactItem = item-&gt;contactItem();
219 contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "FirstName", iFirstName-&gt;<a href="qlineedit.html#text">text</a>() );
220 contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "LastName", iLastName-&gt;<a href="qlineedit.html#text">text</a>() );
221 contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "HomeAddress", iAddress-&gt;<a href="qlineedit.html#text">text</a>() );
222 contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "Email1Address", iEMail-&gt;<a href="qlineedit.html#text">text</a>() );
223 contactItem-&gt;<a href="qaxbase.html#dynamicCall">dynamicCall</a>( "Save()" );
224
225 <a name="x2728"></a> item-&gt;<a href="qlistviewitem.html#setText">setText</a>( 0, iFirstName-&gt;<a href="qlineedit.html#text">text</a>() );
226 item-&gt;<a href="qlistviewitem.html#setText">setText</a>( 1, iLastName-&gt;<a href="qlineedit.html#text">text</a>() );
227 item-&gt;<a href="qlistviewitem.html#setText">setText</a>( 2, iAddress-&gt;<a href="qlineedit.html#text">text</a>() );
228 item-&gt;<a href="qlistviewitem.html#setText">setText</a>( 3, iEMail-&gt;<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>
233library. 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 &copy; 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>
Note: See TracBrowser for help on using the repository browser.