歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Qt插件系統的概要實例介紹

Qt插件系統的概要實例介紹

日期:2017/3/1 10:24:48   编辑:Linux編程
一、 對於每一種類型的插件,通常至少需要兩個類:
(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. virtual void clearCalibration() {}
  8. virtual void calibrate(const QWSPointerCalibrationData *) {}
  9. virtual void getCalibration(QWSPointerCalibrationData *) const {}
  10. virtual void resume() = 0;
  11. virtual void suspend() = 0;
  12. void limitToScreen(QPoint &pt);
  13. void mouseChanged(const QPoint& pos, int bstate, int wheel = 0);
  14. const QPoint &pos() const { return mousePos; }
  15. void setScreen(const QScreen *screen);
  16. protected:
  17. QPoint &mousePos;
  18. QWSMouseHandlerPrivate *d_ptr;
  19. }

該類實現了在嵌入式平台上,一個鼠標設備所具有的屬性和方法。


(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. #if !defined(Q_OS_WIN32) || defined(QT_MAKEDLL)
  40. #ifndef QT_NO_LIBRARY
  41. if (QWSMouseHandlerFactoryInterface *factory = qobject_cast<QWSMouseHandlerFactoryInterface*>(loader()->instance(driver)))
  42. return factory->create(driver, device); /*注意:這個地方就會創建QMouseDriverPlugin實現的自定義插件類*/
  43. #endif
  44. #endif
  45. return 0;
  46. }
注意: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. \brief The QMouseDriverPlugin class is an abstract base class for
  6. mouse driver plugins in Qt for Embedded Linux.
  7. Note that this class is only available in \l{Qt for Embedded Linux}.
  8. \l{Qt for Embedded Linux} provides ready-made drivers for several mouse
  9. protocols, see the \l{Qt for Embedded Linux Pointer Handling}{pointer
  10. handling} documentation for details. Custom mouse drivers can be
  11. implemented by subclassing the QWSMouseHandler class and creating
  12. a mouse driver plugin.
  13. A mouse driver plugin can be created by subclassing
  14. QMouseDriverPlugin and reimplementing the pure virtual keys() and
  15. create() functions. By exporting the derived class using the
  16. Q_EXPORT_PLUGIN2() macro, The default implementation of the
  17. QMouseDriverFactory class will automatically detect the plugin and
  18. load the driver into the server application at run-time. See \l
  19. {How to Create Qt Plugins} for details.
  20. \sa QWSMouseHandler, QMouseDriverFactory
  21. */
  22. /*!
  23. \fn QStringList QMouseDriverPlugin::keys() const
  24. Implement this function to return the list of valid keys, i.e. the
  25. mouse drivers supported by this plugin.
  26. \l{Qt for Embedded Linux} provides ready-made drivers for several mouse
  27. protocols, see the \l {Qt for Embedded Linux Pointer Handling}{pointer
  28. handling} documentation for details.
  29. \sa create()
  30. */
  31. /*!
  32. Constructs a mouse driver plugin with the given \a parent.
  33. Note that this constructor is invoked automatically by the
  34. Q_EXPORT_PLUGIN2() macro, so there is no need for calling it
  35. explicitly.
  36. */

具體的創建plugin的實例參見:

《How to Create Qt Plugins》

《Qt for Embedded Linux Pointer Handling》
Copyright © Linux教程網 All Rights Reserved