Qt插件系统的概要实例介绍

27 篇文章 1 订阅
一、  对于每一种类型的插件,通常至少需要两个类:
                    (1.) 一个是插件封装器类,它实现了插件通用的API函数;
                                比如QWSMouseHandler,其定义如下:                 
   class Q_GUI_EXPORT QWSMouseHandler
{
public:
    explicit QWSMouseHandler(const QString &driver = QString(),
                             const QString &device = QString());
    virtual ~QWSMouseHandler();


    virtual void clearCalibration() {}
    virtual void calibrate(const QWSPointerCalibrationData *) {}
    virtual void getCalibration(QWSPointerCalibrationData *) const {}


    virtual void resume() = 0;
    virtual void suspend() = 0;


    void limitToScreen(QPoint &pt);
    void mouseChanged(const QPoint& pos, int bstate, int wheel = 0);
    const QPoint &pos() const { return mousePos; }


    void setScreen(const QScreen *screen);


protected:
    QPoint &mousePos;
    QWSMouseHandlerPrivate *d_ptr;
}

                                

该类实现了在嵌入式平台上,一个鼠标设备所具有的属性和方法。


                     (2.)一个是一个或多个 处理器类,每个处理器类都实现了一种用于特殊类型插件的API;
                                比如QWSLinuxInputMouseHandler,两者的关系如下:
                          QWSLinuxInputMouseHandler --> QWSCalibratedMouseHandler --> QWSMouseHandler
        通过封装器类才能访问这些处理器类。
       除此之外往往还会有一个名称含有"Factory"的类来在程序运行的时候自动创建合适的插件对象。

       比如,QMouseDriverFactory,其定义如下:

      class Q_GUI_EXPORT QMouseDriverFactory
{
public:
    static QStringList keys();
    static QWSMouseHandler *create(const QString&, const QString &);
};
create的实现如下:
QWSMouseHandler *QMouseDriverFactory::create(const QString& key, const QString &device){
    QString driver = key.toLower();
#if defined(Q_OS_QNX) && !defined(QT_NO_QWS_MOUSE_QNX)
    if (driver == QLatin1String("qnx") || driver.isEmpty())
        return new QQnxMouseHandler(key, device);
#endif
#if defined(Q_OS_INTEGRITY) && !defined(QT_NO_MOUSE_INTEGRITY)
    if (driver == QLatin1String("integrity") || driver.isEmpty())
        return new QIntMouseHandler(key, device);
#endif
#ifndef QT_NO_QWS_MOUSE_LINUXTP
    if (driver == QLatin1String("linuxtp") || driver.isEmpty())
        return new QWSLinuxTPMouseHandler(key, device);
#endif
#ifndef QT_NO_QWS_MOUSE_PC
    if (driver == QLatin1String("auto")
        || driver == QLatin1String("intellimouse")
        || driver == QLatin1String("microsoft")
        || driver == QLatin1String("mousesystems")
        || driver == QLatin1String("mouseman")
        || driver.isEmpty()) {
        return new QWSPcMouseHandler(key, device);
    }
#endif
#ifndef QT_NO_QWS_MOUSE_TSLIB
    if (driver == QLatin1String("tslib") || driver.isEmpty())
        return new QWSTslibMouseHandler(key, device);
#endif
# ifndef QT_NO_QWS_MOUSE_LINUXINPUT
    if (driver == QLatin1String("linuxinput") || \
        driver == QLatin1String("usb") || \
        driver == QLatin1String("linuxis"))
        return new QWSLinuxInputMouseHandler(device);
# endif
#ifndef QT_NO_QWS_MOUSE_QVFB
    if (driver == QLatin1String("qvfbmouse") || driver == QLatin1String("qvfb"))
        return new QVFbMouseHandler(key, device);
#endif

#if !defined(Q_OS_WIN32) || defined(QT_MAKEDLL)
#ifndef QT_NO_LIBRARY
    if (QWSMouseHandlerFactoryInterface *factory = qobject_cast<QWSMouseHandlerFactoryInterface*>(loader()->instance(driver)))
        return factory->create(driver, device);     /*注意:这个地方就会创建QMouseDriverPlugin实现的自定义插件类*/
#endif
#endif
    return 0;
}

注意:QMouseDriverFactory用于探测并且实例化可用的鼠标驱动,Embeded QT for Linux 在server application运行的时候加载合适的鼠标驱动。  Embeded QT for Linux提供几个内置的类来实现某些鼠标驱动,比如QWSLinuxInputMouseHandler ;另外,也支持使用Qt的插件机制来实现自定义的驱动程序,驱动程序的编写也要继承QWSMouseHandler来具体的进行实现。


二、使用QMouseDriverPlugin来创建自定义的鼠标驱动程序

      下面的文字来自于Qt的源文档:
/*!
    \class QMouseDriverPlugin
    \ingroup plugins
    \ingroup qws

    \brief The QMouseDriverPlugin class is an abstract base class for
    mouse driver plugins in Qt for Embedded Linux.

    Note that this class is only available in \l{Qt for Embedded Linux}.

    \l{Qt for Embedded Linux} provides ready-made drivers for several mouse
    protocols, see the \l{Qt for Embedded Linux Pointer Handling}{pointer
    handling} documentation for details. Custom mouse drivers can be
    implemented by subclassing the QWSMouseHandler class and creating
    a mouse driver plugin.

    A mouse driver plugin can be created by subclassing
    QMouseDriverPlugin and reimplementing the pure virtual keys() and
    create() functions. By exporting the derived class using the
    Q_EXPORT_PLUGIN2() macro, The default implementation of the
    QMouseDriverFactory class will automatically detect the plugin and
    load the driver into the server application at run-time. See \l
    {How to Create Qt Plugins} for details.

    \sa QWSMouseHandler, QMouseDriverFactory
*/
/*!
    \fn QStringList QMouseDriverPlugin::keys() const

    Implement this function to return the list of valid keys, i.e. the
    mouse drivers supported by this plugin.

    \l{Qt for Embedded Linux} provides ready-made drivers for several mouse
    protocols, see the \l {Qt for Embedded Linux Pointer Handling}{pointer
    handling} documentation for details.

    \sa create()
*/
/*!
    Constructs a mouse driver plugin with the given \a parent.

    Note that this constructor is invoked automatically by the
    Q_EXPORT_PLUGIN2() macro, so there is no need for calling it
    explicitly.
*/

具体的创建plugin的实例参见:

             《How to Create Qt Plugins》

          《Qt for Embedded Linux Pointer Handling》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值