Hierarchy Example (ActiveQt)
The Hierarchy example is shows how to write an in-process ActiveX control. The control is a QWidget subclass with child widgets that are accessible as sub-types.
The QParentWidget class provides slots to create a widget with a name, and to return a pointer to a named widget. The class declaration uses Q_CLASSINFO() to provide the COM identifiers for this class.
The constructor of QParentWidget creates a vertical box layout. New child widgets are automatically added to the layout.
The createSubWidget slot creates a new QSubWidget with the name provided in the parameter, and sets the label to that name. The widget is also shown explicitly.
The subWidget slot uses the QObject::child() function and returns the first child of type QSubWidget that has the requested name.
The QSubWidget class has a single string-property label, and implements the paintEvent to draw the label. The class uses again Q_CLASSINFO to provide the COM identifiers, and also sets the ToSuperClass attribute to QSubWidget, to ensure that only no slots of any superclasses (i.e. QWidget) are exposed.
The implementation of the QSubWidget class is self-explanatory.
The classes are then exported using a QAxFactory. QParentWidget is exported as a full class (which can be created ), while QSubWidget is only exported as a type, which can only be created indirectly through APIs of QParentWidget.
To build the example you must first build the QAxServer library. Then run qmake and your make tool in examples/activeqt/multiple.
The demonstration requires your WebBrowser to support ActiveX controls, and scripting to be enabled.
<script language="javascript">
function createSubWidget( form )
{
ParentWidget.createSubWidget( form.nameEdit.value );
}
function renameSubWidget( form )
{
var SubWidget = ParentWidget.subWidget( form.nameEdit.value );
if ( !SubWidget ) {
alert( "No such widget " + form.nameEdit.value + "!" );
return;
}
SubWidget.label = form.labelEdit.value;
form.nameEdit.value = SubWidget.label;
}
function setFont( form )
{
ParentWidget.font = form.fontEdit.value;
}
</script>
<p>
This widget can have many children!
</p>
<object ID="ParentWidget" CLASSID="CLSID:d574a747-8016-46db-a07c-b2b4854ee75c"
CODEBASE="http://www.trolltech.com/demos/hierarchy.cab">
[Object not available! Did you forget to build and register the server?]
</object><br />
<form>
<input type="edit" ID="nameEdit" value="<enter object name>" />
<input type="button" value="Create" onClick="createSubWidget(this.form)" />
<input type="edit" ID="labelEdit" />
<input type="button" value="Rename" onClick="renameSubWidget(this.form)" />
<br />
<input type="edit" ID="fontEdit" value="MS Sans Serif" />
<input type="button" value = "Set Font" onClick="setFont(this.form)" />
</form>
|