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/hierarchy/hierarchy.doc:47 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Qt Widget Hierarchy (in-process)</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>Qt Widget Hierarchy (in-process)</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 | The ActiveX control in this example is a <a href="qwidget.html">QWidget</a>
|
---|
37 | subclass with child widgets that are accessible as sub types.
|
---|
38 | <p>
|
---|
39 |
|
---|
40 | <pre> class QParentWidget : public <a href="qwidget.html">QWidget</a>
|
---|
41 | {
|
---|
42 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
43 | public:
|
---|
44 | QParentWidget( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0, WFlags f = 0 );
|
---|
45 |
|
---|
46 | <a href="qsize.html">QSize</a> sizeHint() const;
|
---|
47 |
|
---|
48 | public slots:
|
---|
49 | void createSubWidget( const <a href="qstring.html">QString</a> &name );
|
---|
50 |
|
---|
51 | QSubWidget *subWidget( const <a href="qstring.html">QString</a> &name );
|
---|
52 |
|
---|
53 | private:
|
---|
54 | <a href="qvboxlayout.html">QVBoxLayout</a> *vbox;
|
---|
55 | };
|
---|
56 | </pre>The <tt>QParentWidget</tt> class provides slots to create a widget
|
---|
57 | with a name, and to return a pointer to a named widget.
|
---|
58 | <p>
|
---|
59 |
|
---|
60 | <pre> QParentWidget::QParentWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name, WFlags f )
|
---|
61 | : <a href="qwidget.html">QWidget</a>( parent, name, f )
|
---|
62 | {
|
---|
63 | vbox = new <a href="qvboxlayout.html">QVBoxLayout</a>( this );
|
---|
64 | <a name="x2649"></a> vbox-><a href="qlayout.html#setAutoAdd">setAutoAdd</a>( TRUE );
|
---|
65 | }
|
---|
66 | </pre>The constructor of QParentWidget creates a vertical box layout.
|
---|
67 | New child widgets are automatically added to the layout.
|
---|
68 | <p> <pre> void QParentWidget::createSubWidget( const <a href="qstring.html">QString</a> &name )
|
---|
69 | {
|
---|
70 | QSubWidget *sw = new QSubWidget( this, name );
|
---|
71 | sw->setLabel( name );
|
---|
72 | sw-><a href="qwidget.html#show">show</a>();
|
---|
73 | }
|
---|
74 | </pre>The <tt>createSubWidget</tt> slot creates a new <tt>QSubWidget</tt> with
|
---|
75 | the name provided in the parameter, and sets the label to that
|
---|
76 | name. The widget is also shown explicitly.
|
---|
77 | <p> <pre> QSubWidget *QParentWidget::subWidget( const <a href="qstring.html">QString</a> &name )
|
---|
78 | {
|
---|
79 | return (QSubWidget*)<a href="qobject.html#child">child</a>( name, "QSubWidget" );
|
---|
80 | }
|
---|
81 | </pre>The <tt>subWidget</tt> slot uses the <a href="qobject.html#child">QObject::child</a>() function and
|
---|
82 | returns the first child of type <tt>QSubWidget</tt> that has the requested
|
---|
83 | name.
|
---|
84 | <p>
|
---|
85 |
|
---|
86 | <pre> class QSubWidget : public <a href="qwidget.html">QWidget</a>
|
---|
87 | {
|
---|
88 | Q_OBJECT
|
---|
89 | Q_PROPERTY( QString label READ label WRITE setLabel )
|
---|
90 | public:
|
---|
91 | QSubWidget( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0, WFlags f = 0 );
|
---|
92 |
|
---|
93 | void setLabel( const <a href="qstring.html">QString</a> &text );
|
---|
94 | <a href="qstring.html">QString</a> label() const;
|
---|
95 |
|
---|
96 | <a href="qsize.html">QSize</a> sizeHint() const;
|
---|
97 |
|
---|
98 | protected:
|
---|
99 | void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *e );
|
---|
100 |
|
---|
101 | private:
|
---|
102 | <a href="qstring.html">QString</a> lbl;
|
---|
103 | };
|
---|
104 | </pre>The <tt>QSubWidget</tt> class has a single string-property <tt>label</tt>,
|
---|
105 | and implements the paintEvent to draw the label.
|
---|
106 | <p>
|
---|
107 |
|
---|
108 | <pre> QSubWidget::QSubWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name, WFlags f )
|
---|
109 | : <a href="qwidget.html">QWidget</a>( parent, name, f )
|
---|
110 | {
|
---|
111 | }
|
---|
112 |
|
---|
113 | void QSubWidget::setLabel( const <a href="qstring.html">QString</a> &text )
|
---|
114 | {
|
---|
115 | lbl = text;
|
---|
116 | <a href="qobject.html#setName">setName</a>( text );
|
---|
117 | <a href="qwidget.html#update">update</a>();
|
---|
118 | }
|
---|
119 |
|
---|
120 | QString QSubWidget::label() const
|
---|
121 | {
|
---|
122 | return lbl;
|
---|
123 | }
|
---|
124 |
|
---|
125 | QSize QSubWidget::<a href="qwidget.html#sizeHint">sizeHint</a>() const
|
---|
126 | {
|
---|
127 | <a href="qfontmetrics.html">QFontMetrics</a> fm( <a href="qwidget.html#font">font</a>() );
|
---|
128 | return QSize( fm.<a href="qfontmetrics.html#width">width</a>(lbl), fm.<a href="qfontmetrics.html#height">height</a>() );
|
---|
129 | }
|
---|
130 |
|
---|
131 | void QSubWidget::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * )
|
---|
132 | {
|
---|
133 | <a href="qpainter.html">QPainter</a> painter(this);
|
---|
134 | painter.<a href="qpainter.html#setPen">setPen</a>( <a href="qwidget.html#colorGroup">colorGroup</a>().text() );
|
---|
135 | painter.<a href="qpainter.html#drawText">drawText</a>( <a href="qwidget.html#rect">rect</a>(), AlignCenter, lbl );
|
---|
136 | }
|
---|
137 | </pre>The implementation of the QSubWidget class is self-explanatory.
|
---|
138 | <p>
|
---|
139 |
|
---|
140 | <pre> class ActiveQtFactory : public <a href="qaxfactory.html">QAxFactory</a>
|
---|
141 | {
|
---|
142 | public:
|
---|
143 | ActiveQtFactory( const <a href="quuid.html">QUuid</a> &lib, const <a href="quuid.html">QUuid</a> &app )
|
---|
144 | : <a href="qaxfactory.html">QAxFactory</a>( lib, app )
|
---|
145 | {}
|
---|
146 | <a href="qstringlist.html">QStringList</a> featureList() const
|
---|
147 | {
|
---|
148 | <a href="qstringlist.html">QStringList</a> list;
|
---|
149 | list << "QParentWidget";
|
---|
150 | list << "QSubWidget";
|
---|
151 | return list;
|
---|
152 | }
|
---|
153 | </pre>The <tt>ActiveQtFactory</tt> class implements a <a href="qaxfactory.html">QAxFactory</a>. It returns
|
---|
154 | the class names of all supported types, <tt>QParentWidget</tt> and
|
---|
155 | <tt>QSubWidget</tt>, from the <tt>featureList()</tt> reimplementation.
|
---|
156 | <p> <pre> <a href="qwidget.html">QWidget</a> *create( const <a href="qstring.html">QString</a> &key, QWidget *parent, const char *name )
|
---|
157 | {
|
---|
158 | if ( key == "QParentWidget" )
|
---|
159 | return new QParentWidget( parent, name );
|
---|
160 |
|
---|
161 | return 0;
|
---|
162 | }
|
---|
163 | </pre>The factory can however only create objects of the <tt>QParentWidget</tt>
|
---|
164 | type directly - objects of subtypes can only be created through the
|
---|
165 | interface of <tt>QParentWidget</tt> objects.
|
---|
166 | <p> <pre> <a href="quuid.html">QUuid</a> classID( const <a href="qstring.html">QString</a> &key ) const
|
---|
167 | {
|
---|
168 | if ( key == "QParentWidget" )
|
---|
169 | return QUuid( "{d574a747-8016-46db-a07c-b2b4854ee75c}" );
|
---|
170 | if ( key == "QSubWidget" )
|
---|
171 | return QUuid( "{850652f4-8f71-4f69-b745-bce241ccdc30}" );
|
---|
172 |
|
---|
173 | return QUuid();
|
---|
174 | }
|
---|
175 | <a href="quuid.html">QUuid</a> interfaceID( const <a href="qstring.html">QString</a> &key ) const
|
---|
176 | {
|
---|
177 | if ( key == "QParentWidget" )
|
---|
178 | return QUuid( "{4a30719d-d9c2-4659-9d16-67378209f822}" );
|
---|
179 | if ( key == "QSubWidget" )
|
---|
180 | return QUuid( "{2d76cc2f-3488-417a-83d6-debff88b3c3f}" );
|
---|
181 |
|
---|
182 | return QUuid();
|
---|
183 | }
|
---|
184 | <a href="quuid.html">QUuid</a> eventsID( const <a href="qstring.html">QString</a> &key ) const
|
---|
185 | {
|
---|
186 | if ( key == "QParentWidget" )
|
---|
187 | return QUuid( "{aac9f855-c3dc-4cae-b747-c77f4d509f4c}" );
|
---|
188 | if ( key == "QSubWidget" )
|
---|
189 | return QUuid( "{25fac47e-c723-4696-8c74-6185903bdf65}" );
|
---|
190 |
|
---|
191 | return QUuid();
|
---|
192 | }
|
---|
193 | </pre>COM however requires the IDs for the interfaces of the sub types as
|
---|
194 | well to be able to marshal calls correctly.
|
---|
195 | <p> <pre> <a href="qstring.html">QString</a> exposeToSuperClass( const <a href="qstring.html">QString</a> &key ) const
|
---|
196 | {
|
---|
197 | if ( key == "QSubWidget" )
|
---|
198 | return key;
|
---|
199 | return QAxFactory::exposeToSuperClass(key);
|
---|
200 | }
|
---|
201 | };
|
---|
202 | </pre>Objects of the <tt>QSubWidget</tt> type should not expose the full
|
---|
203 | functionality of e.g. <a href="qwidget.html">QWidget</a>. Only those properties and slots
|
---|
204 | explicitly declared in the type are accessible.
|
---|
205 | <p> <pre> QAXFACTORY_EXPORT( ActiveQtFactory, "{9e626211-be62-4d18-9483-9419358fbb03}", "{75c276de-1df5-451f-a004-e4fa1a587df1}" )
|
---|
206 | </pre>The factory is then exported using the <a href="qaxfactory.html#QAXFACTORY_EXPORT">QAXFACTORY_EXPORT</a>
|
---|
207 | macro.
|
---|
208 | <p> To build the example you must first build the <a href="qaxserver.html">QAxServer</a> library. Then run qmake and your make tool in
|
---|
209 | <tt>examples/multiple</tt>.
|
---|
210 | <p> <hr>
|
---|
211 | <p> The <a href="qaxserver-demo-hierarchy.html">demonstration</a> requires your
|
---|
212 | WebBrowser to support ActiveX controls, and scripting to be enabled.
|
---|
213 | <p>
|
---|
214 |
|
---|
215 | <pre> <script language=javascript>
|
---|
216 | function createSubWidget( form )
|
---|
217 | {
|
---|
218 | ParentWidget.createSubWidget( form.nameEdit.value );
|
---|
219 | }
|
---|
220 |
|
---|
221 | function renameSubWidget( form )
|
---|
222 | {
|
---|
223 | var SubWidget = ParentWidget.subWidget( form.nameEdit.value );
|
---|
224 | if ( !SubWidget ) {
|
---|
225 | alert( "No such widget " + form.nameEdit.value + "!" );
|
---|
226 | return;
|
---|
227 | }
|
---|
228 | SubWidget.label = form.labelEdit.value;
|
---|
229 | form.nameEdit.value = SubWidget.label;
|
---|
230 | }
|
---|
231 |
|
---|
232 | function setFont( form )
|
---|
233 | {
|
---|
234 | ParentWidget.font = form.fontEdit.value;
|
---|
235 | }
|
---|
236 | </script>
|
---|
237 |
|
---|
238 | <p>
|
---|
239 | This widget can have many children!<br>
|
---|
240 | <object ID="ParentWidget" CLASSID="CLSID:d574a747-8016-46db-a07c-b2b4854ee75c"
|
---|
241 | CODEBASE=http://www.trolltech.com/demos/hierarchy.cab>
|
---|
242 | [Object not available! Did you forget to build and register the server?]
|
---|
243 | </object><br>
|
---|
244 | <form>
|
---|
245 | <input type="edit" ID="nameEdit" value = "<enter object name>">
|
---|
246 | <input type="button" value = "Create" onClick="createSubWidget(this.form)">
|
---|
247 | <input type="edit" ID="labelEdit">
|
---|
248 | <input type="button" value = "Rename" onClick="renameSubWidget(this.form)">
|
---|
249 | <br>
|
---|
250 | <input type="edit" ID="fontEdit" value = "MS Sans Serif">
|
---|
251 | <input type="button" value = "Set Font" onClick="setFont(this.form)">
|
---|
252 | </form>
|
---|
253 | </pre><p>See also <a href="qaxserver-examples.html">The QAxServer Examples</a>.
|
---|
254 |
|
---|
255 | <!-- eof -->
|
---|
256 | <p><address><hr><div align=center>
|
---|
257 | <table width=100% cellspacing=0 border=0><tr>
|
---|
258 | <td>Copyright © 2007
|
---|
259 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
260 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
261 | </table></div></address></body>
|
---|
262 | </html>
|
---|