Qt插件

一、  对于每一种类型的插件,通常至少需要两个类:
(1.) 一个是插件封装器类,它实现了插件通用的API函数;
比如QWSMouseHandler,其定义如下:                 
  1.    class Q_GUI_EXPORT QWSMouseHandler  
  2. {  
  3. public:  
  4.     explicit QWSMouseHandler(const QString &driver = QString(),  
  5.                              const QString &device = QString());  
  6.     virtual ~QWSMouseHandler();  
  7.   
  8.   
  9.     virtual void clearCalibration() {}  
  10.     virtual void calibrate(const QWSPointerCalibrationData *) {}  
  11.     virtual void getCalibration(QWSPointerCalibrationData *) const {}  
  12.   
  13.   
  14.     virtual void resume() = 0;  
  15.     virtual void suspend() = 0;  
  16.   
  17.   
  18.     void limitToScreen(QPoint &pt);  
  19.     void mouseChanged(const QPoint& pos, int bstate, int wheel = 0);  
  20.     const QPoint &pos() const { return mousePos; }  
  21.   
  22.   
  23.     void setScreen(const QScreen *screen);  
  24.   
  25.   
  26. protected:  
  27.     QPoint &mousePos;  
  28.     QWSMouseHandlerPrivate *d_ptr;  
  29. }  

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


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

       比如,QMouseDriverFactory,其定义如下:

  1.       class Q_GUI_EXPORT QMouseDriverFactory  
  2. {  
  3. public:  
  4.     static QStringList keys();  
  5.     static QWSMouseHandler *create(const QString&, const QString &);  
  6. };  
create的实现如下:
  1. QWSMouseHandler *QMouseDriverFactory::create(const QString& key, const QString &device){  
  2.     QString driver = key.toLower();  
  3. #if defined(Q_OS_QNX) && !defined(QT_NO_QWS_MOUSE_QNX)   
  4.     if (driver == QLatin1String("qnx") || driver.isEmpty())  
  5.         return new QQnxMouseHandler(key, device);  
  6. #endif   
  7. #if defined(Q_OS_INTEGRITY) && !defined(QT_NO_MOUSE_INTEGRITY)   
  8.     if (driver == QLatin1String("integrity") || driver.isEmpty())  
  9.         return new QIntMouseHandler(key, device);  
  10. #endif   
  11. #ifndef QT_NO_QWS_MOUSE_LINUXTP   
  12.     if (driver == QLatin1String("linuxtp") || driver.isEmpty())  
  13.         return new QWSLinuxTPMouseHandler(key, device);  
  14. #endif   
  15. #ifndef QT_NO_QWS_MOUSE_PC   
  16.     if (driver == QLatin1String("auto")  
  17.         || driver == QLatin1String("intellimouse")  
  18.         || driver == QLatin1String("microsoft")  
  19.         || driver == QLatin1String("mousesystems")  
  20.         || driver == QLatin1String("mouseman")  
  21.         || driver.isEmpty()) {  
  22.         return new QWSPcMouseHandler(key, device);  
  23.     }  
  24. #endif   
  25. #ifndef QT_NO_QWS_MOUSE_TSLIB   
  26.     if (driver == QLatin1String("tslib") || driver.isEmpty())  
  27.         return new QWSTslibMouseHandler(key, device);  
  28. #endif   
  29. # ifndef QT_NO_QWS_MOUSE_LINUXINPUT   
  30.     if (driver == QLatin1String("linuxinput") || \  
  31.         driver == QLatin1String("usb") || \  
  32.         driver == QLatin1String("linuxis"))  
  33.         return new QWSLinuxInputMouseHandler(device);  
  34. # endif   
  35. #ifndef QT_NO_QWS_MOUSE_QVFB   
  36.     if (driver == QLatin1String("qvfbmouse") || driver == QLatin1String("qvfb"))  
  37.         return new QVFbMouseHandler(key, device);  
  38. #endif   
  39.   
  40. #if !defined(Q_OS_WIN32) || defined(QT_MAKEDLL)   
  41. #ifndef QT_NO_LIBRARY   
  42.     if (QWSMouseHandlerFactoryInterface *factory = qobject_cast<QWSMouseHandlerFactoryInterface*>(loader()->instance(driver)))  
  43.         return factory->create(driver, device);     /*注意:这个地方就会创建QMouseDriverPlugin实现的自定义插件类*/  
  44. #endif   
  45. #endif   
  46.     return 0;  
  47. }  
注意:QMouseDriverFactory用于探测并且实例化可用的鼠标驱动,Embeded QT for Linux 在server application运行的时候加载合适的鼠标驱动。  Embeded QT for Linux提供几个内置的类来实现某些鼠标驱动,比如QWSLinuxInputMouseHandler ;另外,也支持使用Qt的插件机制来实现自定义的驱动程序,驱动程序的编写也要继承QWSMouseHandler来具体的进行实现。


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

下面的文字来自于Qt的源文档:
  1. /*!  
  2.     \class QMouseDriverPlugin  
  3.     \ingroup plugins  
  4.     \ingroup qws  
  5.   
  6.     \brief The QMouseDriverPlugin class is an abstract base class for  
  7.     mouse driver plugins in Qt for Embedded Linux.  
  8.   
  9.     Note that this class is only available in \l{Qt for Embedded Linux}.  
  10.   
  11.     \l{Qt for Embedded Linux} provides ready-made drivers for several mouse  
  12.     protocols, see the \l{Qt for Embedded Linux Pointer Handling}{pointer  
  13.     handling} documentation for details. Custom mouse drivers can be  
  14.     implemented by subclassing the QWSMouseHandler class and creating  
  15.     a mouse driver plugin.  
  16.   
  17.     A mouse driver plugin can be created by subclassing  
  18.     QMouseDriverPlugin and reimplementing the pure virtual keys() and  
  19.     create() functions. By exporting the derived class using the  
  20.     Q_EXPORT_PLUGIN2() macro, The default implementation of the  
  21.     QMouseDriverFactory class will automatically detect the plugin and  
  22.     load the driver into the server application at run-time. See \l  
  23.     {How to Create Qt Plugins} for details.  
  24.   
  25.     \sa QWSMouseHandler, QMouseDriverFactory  
  26. */  
  27. /*!  
  28.     \fn QStringList QMouseDriverPlugin::keys() const  
  29.   
  30.     Implement this function to return the list of valid keys, i.e. the  
  31.     mouse drivers supported by this plugin.  
  32.   
  33.     \l{Qt for Embedded Linux} provides ready-made drivers for several mouse  
  34.     protocols, see the \l {Qt for Embedded Linux Pointer Handling}{pointer  
  35.     handling} documentation for details.  
  36.   
  37.     \sa create()  
  38. */  
  39. /*!  
  40.     Constructs a mouse driver plugin with the given \a parent.  
  41.   
  42.     Note that this constructor is invoked automatically by the  
  43.     Q_EXPORT_PLUGIN2() macro, so there is no need for calling it  
  44.     explicitly.  
  45. */  

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

             《How to Create Qt Plugins》

          《Qt for Embedded Linux Pointer Handling》
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值