Qt插件系统的概要实例介绍
一、 对于每一种类型的插件,通常至少需要两个类:
(1.) 一个是插件封装器类,它实现了插件通用的API函数;
比如QWSMouseHandler,其定义如下:
[cpp]
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,其定义如下:
[cpp]
class Q_GUI_EXPORT QMouseDriverFactory
{
public:
static QStringList keys();
static QWSMouseHandler *create(const QString&, const QString &);
};
create的实现如下:
[cpp]
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 (QWSMouseHandlerFactoryInte易做图ce *factory = qobject_cast<QWSMouseHandlerFactoryInte易做图ce*>(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的源文档:
[plain]
/*!
\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 severa
补充:软件开发 , 其他 ,