QAxFactory Class

  • Header: QAxFactory

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS AxServer)

    target_link_libraries(mytarget PRIVATE Qt6::AxServer)

  • qmake: QT += axserver

  • Inherits:

  • Inherited By:

Detailed Description

Implement this factory once in your COM server to provide information about the components the server can create. Subclass QAxFactory and implement the pure virtual functions in any implementation file (e.g. main.cpp), and export the factory using the QAXFACTORY_EXPORT() macro.

 
Sélectionnez
QStringList ActiveQtFactory::featureList() const
{
    QStringList list;
    list << "ActiveX1";
    list << "ActiveX2";
    return list;
}

QObject *ActiveQtFactory::createObject(const QString &key)
{
    if (key == "ActiveX1")
        return new ActiveX1(parent);
    if (key == "ActiveX2")
        return new ActiveX2(parent);
    return 0;
}

const QMetaObject *ActiveQtFactory::metaObject(const QString &key) const
{
    if (key == "ActiveX1")
        return &ActiveX1::staticMetaObject;
    if (key == "ActiveX2")
        return &ActiveX2::staticMetaObject;
}

QUuid ActiveQtFactory::classID(const QString &key) const
{
    if (key == "ActiveX1")
        return "{01234567-89AB-CDEF-0123-456789ABCDEF}";
    ...
    return QUuid();
}

QUuid ActiveQtFactory::interfaceID(const QString &key) const
{
    if (key == "ActiveX1")
        return "{01234567-89AB-CDEF-0123-456789ABCDEF}";
    ...
    return QUuid();
}

QUuid ActiveQtFactory::eventsID(const QString &key) const
{
    if (key == "ActiveX1")
        return "{01234567-89AB-CDEF-0123-456789ABCDEF}";
    ...
    return QUuid();
}

QAXFACTORY_EXPORT(
    ActiveQtFactory,                          // factory class
    "{01234567-89AB-CDEF-0123-456789ABCDEF}", // type library ID
    "{01234567-89AB-CDEF-0123-456789ABCDEF}"  // application ID
)

If you use the Q_CLASSINFO() macro to provide the unique identifiers or other attributes for your class you can use the QAXFACTORY_BEGIN(), QAXCLASS() and QAXFACTORY_END() macros to expose one or more classes as COM objects.

 
Sélectionnez
QAXFACTORY_BEGIN(
    "{01234567-89AB-CDEF-0123-456789ABCDEF}", // type library ID
    "{01234567-89AB-CDEF-0123-456789ABCDEF}"  // application ID
)
    QAXCLASS(Class1)
    QAXCLASS(Class2)
QAXFACTORY_END()

Only one QAxFactory implementation may be instantiated and exported by an ActiveX server application. This instance is accessible through the global qAxFactory() function.

A factory can also reimplement the registerClass() and unregisterClass() functions to set additional flags for an ActiveX control in the registry. To limit the number of methods or properties a widget class exposes from its parent classes reimplement exposeToSuperClass().

See Also

Member Type Documentation

 

enum QAxFactory::ServerType

This enum specifies the different types of servers that can be started with startServer.

Constant

Value

Description

QAxFactory::SingleInstance

0

The server process can create only one instance of each exported class. COM starts a new process for each request. This is typically used in servers that export only one creatable class.

QAxFactory::MultipleInstances

1

The server can create multiple instances of each exported class. This is the default. All instances will live in the same thread, and will share static resources.

Member Function Documentation

 

QAxFactory::QAxFactory(const QUuid &libid, const QUuid &appid)

Constructs a QAxFactory object that returns libid and appid in the implementation of the respective interface functions.

[override virtual] QAxFactory::~QAxFactory()

Destroys the QAxFactory object.

[virtual] QUuid QAxFactory::appID() const

Reimplement this function to return the ActiveX server's application identifier.

[virtual] QUuid QAxFactory::classID(const QString &key) const

Reimplement this function to return the class identifier for each key returned by the featureList() implementation, or an empty QUuid if this factory doesn't support the value of key.

The default implementation interprets key as the class name, and returns the value of the Q_CLASSINFO() entry "ClassID".

[pure virtual] QObject *QAxFactory::createObject(const QString &key)

Reimplement this function to return a new object for key, or 0 if this factory doesn't support the value of key.

If the object returned is a QWidget it will be exposed as an ActiveX control, otherwise the returned object will be exposed as a simple COM object.

[virtual] bool QAxFactory::createObjectWrapper(QObject *object, IDispatch **wrapper)

Reimplement this function to provide the COM object for object in wrapper. Return true if the function was successful; otherwise return false.

The default implementation creates a generic automation wrapper based on the meta object information of object.

[virtual] QUuid QAxFactory::eventsID(const QString &key) const

Reimplement this function to return the identifier of the event interface for each key returned by the featureList() implementation, or an empty QUuid if this factory doesn't support the value of key.

The default implementation interprets key as the class name, and returns the value of the Q_CLASSINFO() entry "EventsID".

[virtual] QString QAxFactory::exposeToSuperClass(const QString &key) const

Reimplement this function to return the name of the super class of key up to which methods and properties should be exposed by the ActiveX control.

The default implementation interprets key as the class name, and returns the value of the Q_CLASSINFO() entry "ToSuperClass". If no such value is set the null-string is returned, and the functions and properties of all the super classes including QWidget will be exposed.

To only expose the functions and properties of the class itself, reimplement this function to return key.

[pure virtual] QStringList QAxFactory::featureList() const

Reimplement this function to return a list of the widgets (class names) supported by this factory.

[virtual] bool QAxFactory::hasStockEvents(const QString &key) const

Reimplement this function to return true if the ActiveX control key should support the standard ActiveX events

  • Click

  • DblClick

  • KeyDown

  • KeyPress

  • KeyUp

  • MouseDown

  • MouseUp

  • MouseMove

The default implementation interprets key as the class name, and returns true if the value of the Q_CLASSINFO() entry "StockEvents" is "yes". Otherwise this function returns false.

[virtual] QUuid QAxFactory::interfaceID(const QString &key) const

Reimplement this function to return the interface identifier for each key returned by the featureList() implementation, or an empty QUuid if this factory doesn't support the value of key.

The default implementation interprets key as the class name, and returns the value of the Q_CLASSINFO() entry "InterfaceID".

[static] bool QAxFactory::isServer()

Returns true if the application has been started (by COM) as an ActiveX server, otherwise returns false.

 
Sélectionnez
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    if (!QAxFactory::isServer()) {
        // initialize for stand-alone execution
    }
    return app.exec();
}

[virtual] bool QAxFactory::isService() const

Reimplement this function to return true if the server is running as a persistent service (e.g. an NT service) and should not terminate even when all objects provided have been released.

The default implementation returns false.

[pure virtual] const QMetaObject *QAxFactory::metaObject(const QString &key) const

Reimplement this function to return the QMetaObject corresponding to key, or 0 if this factory doesn't support the value of key.

[static] bool QAxFactory::registerActiveObject(QObject *object)

Registers the QObject object with COM as a running object, and returns true if the registration succeeded, otherwise returns false. The object is unregistered automatically when it is destroyed.

This function should only be called if the application has been started by the user (i.e. not by COM to respond to a request), and only for one object, usually the toplevel object of the application's object hierarchy.

This function does nothing and returns false if the object's class info for "RegisterObject" is not set to "yes", or if the server is an in-process server.

[virtual] void QAxFactory::registerClass(const QString &key, QSettings *settings) const

Registers additional values for the class key in the system registry using the settings object. The standard values have already been registered by the framework, but additional values, e.g. implemented categories, can be added in an implementation of this function.

 
Sélectionnez
settings->setValue("/CLSID/" + classID(key)
                   + "/Implemented Categories/"
                   + "/{00000000-0000-0000-000000000000}/.",
                   QString());

If you reimplement this function you must also reimplement unregisterClass() to remove the additional registry values.

See Also

See also QSettings

[static] QString QAxFactory::serverDirPath()