07-插件项目各文件的功能实现
12.2.2 插件项目各文件的功能实现
1.QwBatteryPlugin类
qwbatteryplugin.h文件中的内容是对插件类QwBatteryPlugin的定义,类定义完整代码如下:
#include <QDesignerCustomWidgetInterface>
class QwBatteryPlugin : public QObject, public QDesignerCustomWidgetInterface
{ Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)
#if QT_VERSION >= 0x050000
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
#endif // QT_VERSION >= 0x050000
public:
QwBatteryPlugin(QObject *parent = 0);
bool isContainer() const;
bool isInitialized() const;
QIcon icon() const;
QString domXml() const;
QString group() const;
QString includeFile() const;
QString name() const;
QString toolTip() const;
QString whatsThis() const;
QWidget *createWidget(QWidget *parent);
void initialize(QDesignerFormEditorInterface *core);
private:
bool m_initialized;
};
QwBatteryPlugin类实现了QDesignerCustomWidgetInterface接口,这是专门为Qt Designer设计自定义Widget组件的接口。
在这个类定义里,除了Q_OBJECT宏之外,还用了Q_INTERFACES宏声明了实现的接口,用Q_PLUGIN_METADATA声明了元数据名称,这些都无需改动。
public 部分的函数都是有关插件信息或功能的一些函数,通过其实现代码可以看出这些函数的功能。下面是qwbatteryplugin.cpp文件里的实现代码。
#include "qwbattery.h"
#include "qwbatteryplugin.h"
#include <QtPlugin>
QwBatteryPlugin::QwBatteryPlugin(QObject *parent) : QObject(parent)
{ m_initialized = false;
}
void QwBatteryPlugin::initialize(QDesignerFormEditorInterface * /* core */)
{ if (m_initialized)
return;
// Add extension registrations, etc. here
m_initialized = true;
}
bool QwBatteryPlugin::isInitialized() const
{//是否初始化
return m_initialized;
}
QWidget *QwBatteryPlugin::createWidget(QWidget *parent)
{//返回自定义Widget组件的实例
return new QwBattery(parent);
}
QString QwBatteryPlugin::name() const
{//自定义Widget组件类的名称
return QLatin1String("QwBattery");
}
QString QwBatteryPlugin::group() const
{//在组件面板中所属分组名称
return QLatin1String("MyWidget");
}
QIcon QwBatteryPlugin::icon() const
{//图标文件名
return QIcon(QLatin1String(":/44.ico"));
}
QString QwBatteryPlugin::toolTip() const
{//toolTip信息
return QLatin1String("Battery charger indicator");
}
QString QwBatteryPlugin::whatsThis() const
{//whatsThis 信息
return QLatin1String("A battery charger indicator");
}
bool QwBatteryPlugin::isContainer() const
{ //是否作为容器, false表示该组件上不允许再放其他组件
return false;
}
QString QwBatteryPlugin::domXml() const
{//XML文件描述信息
return QLatin1String("<widget class=\"QwBattery\" name=\"qwBattery\">\n</widget>\n");
}
QString QwBatteryPlugin::includeFile() const
{//包含文件名
return QLatin1String("qwbattery.h");
}
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(qwbatteryplugin, QwBatteryPlugin)
#endif // QT_VERSION < 0x050000
这些函数的部分内容是根据创建插件向导里设置的内容自动生成的。createWidget()函数创建一个QwBattery类的实例,在UI设计器里作为设计实例;name()函数返回组件的类名称;group()函数设置组件安装在面板里的分组名称;icon()设置组件的图标;isContainer()设置组件是否作为容器,false表示不作为容器,不能在这个组件上放置其他组件;domXml()函数用XML设置组件的一些属性,缺省的只设置了类名和实例名。
2.QwBatteryPlugin.pro的内容
QwBatteryPlugin.pro是插件项目的项目管理文件,其内容如下:
CONFIG += plugin debug_and_release
TARGET = $$qtLibraryTarget(qwbatteryplugin)
TEMPLATE = lib
HEADERS = qwbatteryplugin.h
SOURCES = qwbatteryplugin.cpp
RESOURCES = icons.qrc
LIBS += -L.
greaterThan(Qt_MAJOR_VERSION, 4) {
Qt += designer
} else {
CONFIG += designer
}
target.path = $$[Qt_INSTALL_PLUGINS]/designer
INSTALLS += target
include(qwbattery.pri)
CONFIG是用于qkame编译设置的,这里配置为:
CONFIG += plugin debug_and_release
其中,plugin表示项目要作为插件,编译后只会产生lib和dll(或.so)文件,debug_and_release 表示项目可以用debug和release模式编译。
TEMPLATE 定义项目的类型,这里设置为:
TEMPLATE = lib
这表示项目是一个库,一般的应用程序模板类型是app。
3.内置项目qwbattery.pri
qwbattery.pri是内置于QwBatteryPlugin.pro中的项目,qwbattery.pri项目配置文件只有两行,也就是这个内置项目中包含的头文件和源文件名称。
HEADERS += qwbattery.h
SOURCES += qwbattery.cpp
4.组件类QwBattery的定义
qwbattery.h里的内容是对组件类QwBattery的类定义,其功能与12.1节中的QmyBattery类完全一样。这两个类的名称之所以不同,是为了在编译两个实例时不产生冲突。
QwBattery类的定义与QmyBattery的定义基本一样,只是在声明类的时候需要加一个宏QDESIGNER_WIDGET_EXPORT,并且用Q_PROPERTY宏定义了一个属性powerLevel。QwBattery类的完整定义如下:
#include <QDesignerExportWidget>
#include <QWidget>
class QDESIGNER_WIDGET_EXPORT QwBattery : public QWidget
{
Q_OBJECT
//自定义属性
Q_PROPERTY(int powerLevel READ powerLevel WRITE setPowerLevel
NOTIFY powerLevelChanged DESIGNABLE true)
private:
QColor mColorBack=Qt::white; //背景颜色
QColor mColorBorder=Qt::black; //电池边框颜色
QColor mColorPower=Qt::green; //电量柱颜色
QColor mColorWarning=Qt::red; //电量短缺时的颜色
int mPowerLevel=60; //电量0-100
int mWarnLevel=20; //电量低警示阈值
protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
public:
explicit QwBattery(QWidget *parent = 0);
void setPowerLevel(int pow);//设置当前电量
int powerLevel();
void setWarnLevel(int warn);//设置电量低阈值
int warnLevel();
QSize sizeHint();//报告缺省大小
signals:
void powerLevelChanged(int );
public slots:
};
QDESIGNER_WIDGET_EXPORT宏用于将自定义组件类从插件导出给Qt Designer使用,必须在类名称前使用此宏。
Q_PROPERTY宏用于定义属性,这里定义了一个int类型的属性powerLevel。READ宏声明了属性的读取函数是powerLevel();WRITE宏声明了设置属性值的函数是setPowerLevel();NOTIFY宏声明了其值变化时发射的信号是powerLevelChanged();DESIGNABLE 宏定义属性在UI设计器里是否可见,缺省为true。
将从QWidget继承的子类QwBattery作为插件安装到UI设计器的组件面板里,则在设计期间就可以从属性编辑器里看到这个powerLevel属性并进行设置。
QwBattery类的实现代码与QmyBattery的实现代码完全相同,不再列出。