1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example activeqt/comapp
|
---|
30 | \title COM App Example (ActiveQt)
|
---|
31 |
|
---|
32 | The COM App example shows how to use ActiveQt to develop a Qt
|
---|
33 | application that can be automated via COM. Different QObject
|
---|
34 | based classes are exposed as COM objects that communicate with the
|
---|
35 | GUI of the running Qt application. The APIs of those COM objects
|
---|
36 | has been designed to resemble the APIs of standard COM
|
---|
37 | applications; i.e. those from Microsoft Office.
|
---|
38 |
|
---|
39 | \snippet examples/activeqt/comapp/main.cpp 2
|
---|
40 | The first class \c Application represents the application object. It
|
---|
41 | exposes read-only properties \c documents and \c id to get access to the
|
---|
42 | list of documents, and an identifier. A read/write property \c visible
|
---|
43 | controls whether the QTabWidget-based user interface of the application
|
---|
44 | should be visible, and a slot \c quit() terminates the application.
|
---|
45 |
|
---|
46 | The \e RegisterObject attribute is set to make sure that instances of this
|
---|
47 | class are registered in COM's running object table (ROT) - this allows COM
|
---|
48 | clients to connect to an already instantiated COM object.
|
---|
49 |
|
---|
50 | \snippet examples/activeqt/comapp/main.cpp 1
|
---|
51 | The \c DocumentList class stores a list of documents. It provides an API
|
---|
52 | to read the number of documents, to access each document by index and to
|
---|
53 | create a new document. The \c application property returns the root object.
|
---|
54 |
|
---|
55 | \snippet examples/activeqt/comapp/main.cpp 0
|
---|
56 |
|
---|
57 | The \c Document class finally represents a document in the application.
|
---|
58 | Each document is represented by a page in the application's tab widget, and
|
---|
59 | has a title that is readable and writable through the document's API.
|
---|
60 | The \c application property again returns the root object.
|
---|
61 |
|
---|
62 | \snippet examples/activeqt/comapp/main.cpp 3
|
---|
63 | The implementation of the \c Document class creates a new page for the tab
|
---|
64 | widget, and uses the title of that page for the title property. The page
|
---|
65 | is deleted when the document is deleted.
|
---|
66 |
|
---|
67 | \snippet examples/activeqt/comapp/main.cpp 4
|
---|
68 | The \c DocumentList implementation is straightforward.
|
---|
69 |
|
---|
70 | \snippet examples/activeqt/comapp/main.cpp 5
|
---|
71 | The \c Application class initializes the user interface in the constructor,
|
---|
72 | and shows and hides it in the implementation of \c setVisible(). The object
|
---|
73 | name (accessible through the \c id property) is set to \c "From QAxFactory"
|
---|
74 | to indicate that this COM object has been created by COM. Note that there is
|
---|
75 | no destructor that would delete the QTabWidget - this is instead done in the
|
---|
76 | \c quit() slot, before calling QApplication::quit() through a single-shot-timer,
|
---|
77 | which is necessary ensure that the COM call to the slot is complete.
|
---|
78 |
|
---|
79 | \snippet examples/activeqt/comapp/main.cpp 6
|
---|
80 | The classes are exported from the server using the QAxFactory macros. Only
|
---|
81 | \c Application objects can be instantiated from outside - the other APIs can
|
---|
82 | only be used after accessing the respective objects throught the \c Application
|
---|
83 | API.
|
---|
84 |
|
---|
85 | \snippet examples/activeqt/comapp/main.cpp 7
|
---|
86 | The main() entry point function creates a QApplication, and just enters the
|
---|
87 | event loop if the application has been started by COM. If the application
|
---|
88 | has been started by the user, then the \c Application object is created and
|
---|
89 | the object name is set to "From Application". Then the COM server is started,
|
---|
90 | and the application object is registered with COM. It is now accessible to
|
---|
91 | COM clients through the client-specific APIs.
|
---|
92 |
|
---|
93 | Application exiting is controlled explicitly - if COM started the application,
|
---|
94 | then the client code has to call quit(); if the user started the application,
|
---|
95 | then the application terminates when the last window has been closed.
|
---|
96 |
|
---|
97 | Finally, the user interface is made visible, and the event loop is started.
|
---|
98 |
|
---|
99 | A simple Visual Basic application could now access this Qt application. In VB,
|
---|
100 | start a new "Standard Exe" project and add a project reference to the comappLib
|
---|
101 | type library. Create a form with a listbox "DocumentList", a static label
|
---|
102 | "DocumentsCount" and a command button "NewDocument". Finally, implement the code
|
---|
103 | for the form like this:
|
---|
104 |
|
---|
105 | \snippet doc/src/snippets/code/doc_src_examples_activeqt_comapp.qdoc 0
|
---|
106 |
|
---|
107 | To build the example you must first build the QAxServer library.
|
---|
108 | Then run \c qmake and your make tool in
|
---|
109 | \c{examples\activeqt\comapp}.
|
---|
110 | */
|
---|