QtDesigner自定义窗口部件有两种方法:改进法(promotion)和插件法(plugin)
改进法
1、改进法之前,要先写好子类化QSpinBox后的HexspinBox.h和HexspinBox.cpp文件。把这两个文件拷贝到想要的项目中。
HexspinBox.h
- #ifndef HEXSPINBOX_H
- #define HEXSPINBOX_H
- #include <QSpinBox>
- class QRegExpValidator;
- class HexSpinBox : public QSpinBox
- {
- Q_OBJECT
- public:
- HexSpinBox(QWidget *parent = 0);
- protected:
- QValidator::State validate(QString &text, int &pos) const;
- int valueFromText(const QString &text) const;
- QString textFromValue(int value) const;
- private:
- QRegExpValidator *validator;
- };
- #endif
#ifndef HEXSPINBOX_H
#define HEXSPINBOX_H
#include <QSpinBox>
class QRegExpValidator;
class HexSpinBox : public QSpinBox
{
Q_OBJECT
public:
HexSpinBox(QWidget *parent = 0);
protected:
QValidator::State validate(QString &text, int &pos) const;
int valueFromText(const QString &text) const;
QString textFromValue(int value) const;
private:
QRegExpValidator *validator;
};
#endif
HexspinBox.cpp
- #include <QtGui>
- #include "hexspinbox.h"
- HexSpinBox::HexSpinBox(QWidget *parent)
- : QSpinBox(parent)
- {
- setRange(0, 255);
- validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this);
- }
- QValidator::State HexSpinBox::validate(QString &text, int &pos) const
- {
- return validator->validate(text, pos);
- }
- int HexSpinBox::valueFromText(const QString &text) const
- {
- bool ok;
- return text.toInt(&ok, 16);
- }
- QString HexSpinBox::textFromValue(int value) const
- {
- return QString::number(value, 16).toUpper();
- }
#include <QtGui>
#include "hexspinbox.h"
HexSpinBox::HexSpinBox(QWidget *parent)
: QSpinBox(parent)
{
setRange(0, 255);
validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this);
}
QValidator::State HexSpinBox::validate(QString &text, int &pos) const
{
return validator->validate(text, pos);
}
int HexSpinBox::valueFromText(const QString &text) const
{
bool ok;
return text.toInt(&ok, 16);
}
QString HexSpinBox::textFromValue(int value) const
{
return QString::number(value, 16).toUpper();
}
2、在需要开发的项目中的窗口中,
1、用Qt Designer创建一个新的窗体main.ui,把控件箱里的QSpinBox添加到窗体中。
2、右击微调框,选择“Promote to ”上下文菜单。
3、在弹出的对话框中,类名处填写“HexSpinBox”,头文件填写“hexspinbox.h”
好了。在ui生成的包含有QSpinBox的控件文件中,ui的源代码里面多了一段
<customwidgets>
<customwidget>
<class>HSpinBox</class>
<extends>QSpinBox</extends>
<header>hspinbox.h</header>
</customwidget>
包含文件变为"hexspinbox.h"。在Qt Designer中,QSpinBox表示的控件为HexSpinBox,并且可以设置所有的QSpinBox的属性。
可以在VS2008中编译一下main.ui文件,从ui_main.h源代码中可以知道,引入的控件是:
- #include <QtGui/QTableWidget>
- #include <QtGui/QToolBar>
- #include <QtGui/QWidget>
- #include "hspinbox.h"
- QT_BEGIN_NAMESPACE
- class Ui_QMainClass
- {
- public:
- QWidget *centralWidget;
- QPushButton *pushButton;
- QTableWidget *tableWidget;
- QSpinBox *spinBox;
- HSpinBox *hspinBox;
#include <QtGui/QTableWidget>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
#include "hspinbox.h"
QT_BEGIN_NAMESPACE
class Ui_QMainClass
{
public:
QWidget *centralWidget;
QPushButton *pushButton;
QTableWidget *tableWidget;
QSpinBox *spinBox;
HSpinBox *hspinBox;
升级法的缺点是不能在Qt Designer中设置自定义控件自己的特有属性,也不能够绘制自己。这些问题可以用插件法解决。
插件法
1.VS中创建Qt4 Design Plugin 工程 ,名称叫custom
自动建立如下几个文件:
自定义控件:custom.h,custom.cpp
插件:customplugin.h,customplugin.cpp
源代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/b8e58c73d55735a892009ab96f02d3f0.jpeg)
custom.h
- #ifndef CUSTOM_H
- #define CUSTOM_H
- #include <QtGui/QWidget>
- #include "ui_test.h"
- class custom : public QWidget
- {
- Q_OBJECT
- public:
- custom(QWidget *parent = 0);
- ~custom();
- private:
- Ui::Form ui;
- };
- #endif // CUSTOM_H
#ifndef CUSTOM_H
#define CUSTOM_H
#include <QtGui/QWidget>
#include "ui_test.h"
class custom : public QWidget
{
Q_OBJECT
public:
custom(QWidget *parent = 0);
~custom();
private:
Ui::Form ui;
};
#endif // CUSTOM_H
custom.cpp
- #include "custom.h"
- custom::custom(QWidget *parent)
- : QWidget(parent)
- {
- ui.setupUi(this);
- }
- custom::~custom()
- {
- }
#include "custom.h"
custom::custom(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
custom::~custom()
{
}
customplugin.h
- #ifndef CUSTOMPLUGIN_H
- #define CUSTOMPLUGIN_H
- #include <QDesignerCustomWidgetInterface>
- class customPlugin : public QObject, public QDesignerCustomWidgetInterface
- {
- Q_OBJECT
- Q_INTERFACES(QDesignerCustomWidgetInterface)
- public:
- customPlugin(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 initialized;
- };
- #endif // CUSTOMPLUGIN_H
#ifndef CUSTOMPLUGIN_H
#define CUSTOMPLUGIN_H
#include <QDesignerCustomWidgetInterface>
class customPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)
public:
customPlugin(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 initialized;
};
#endif // CUSTOMPLUGIN_H
customplugin.cpp
- #include "custom.h"
- #include <QtCore/QtPlugin>
- #include "customplugin.h"
- customPlugin::customPlugin(QObject *parent)
- : QObject(parent)
- {
- initialized = false;
- }
- void customPlugin::initialize(QDesignerFormEditorInterface */*core*/)
- {
- if (initialized)
- return;
- initialized = true;
- }
- bool customPlugin::isInitialized() const
- {
- return initialized;
- }
- QWidget *customPlugin::createWidget(QWidget *parent)
- {
- return new custom(parent);
- }
- QString customPlugin::name() const
- {
- return "custom";
- }
- QString customPlugin::group() const
- {
- return "My Plugins";
- }
- QIcon customPlugin::icon() const
- {
- return QIcon();
- }
- QString customPlugin::toolTip() const
- {
- return QString();
- }
- QString customPlugin::whatsThis() const
- {
- return QString();
- }
- bool customPlugin::isContainer() const
- {
- return false;
- }
- QString customPlugin::domXml() const
- {
- return "<widget class=\"custom\" name=\"custom\">\n"
- " <property name=\"geometry\">\n"
- " <rect>\n"
- " <x>0</x>\n"
- " <y>0</y>\n"
- " <width>100</width>\n"
- " <height>100</height>\n"
- " </rect>\n"
- " </property>\n"
- "</widget>\n";
- }
- QString customPlugin::includeFile() const
- {
- return "custom.h";
- }
- Q_EXPORT_PLUGIN2(custom, customPlugin)
#include "custom.h"
#include <QtCore/QtPlugin>
#include "customplugin.h"
customPlugin::customPlugin(QObject *parent)
: QObject(parent)
{
initialized = false;
}
void customPlugin::initialize(QDesignerFormEditorInterface */*core*/)
{
if (initialized)
return;
initialized = true;
}
bool customPlugin::isInitialized() const
{
return initialized;
}
QWidget *customPlugin::createWidget(QWidget *parent)
{
return new custom(parent);
}
QString customPlugin::name() const
{
return "custom";
}
QString customPlugin::group() const
{
return "My Plugins";
}
QIcon customPlugin::icon() const
{
return QIcon();
}
QString customPlugin::toolTip() const
{
return QString();
}
QString customPlugin::whatsThis() const
{
return QString();
}
bool customPlugin::isContainer() const
{
return false;
}
QString customPlugin::domXml() const
{
return "<widget class=\"custom\" name=\"custom\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>0</x>\n"
" <y>0</y>\n"
" <width>100</width>\n"
" <height>100</height>\n"
" </rect>\n"
" </property>\n"
"</widget>\n";
}
QString customPlugin::includeFile() const
{
return "custom.h";
}
Q_EXPORT_PLUGIN2(custom, customPlugin)
在其cpp的最后必须 添加下面的宏:
- Q_EXPORT_PLUGIN2(customWidgetPlugin, CustomWidgetPlugin) // 第一个参数为插件的名字,第二个是插件类的名字(而不是自定义控件的类名)
2. 新建后,直接编译,会产生如下错误
1>LINK : fatal error LNK1181: cannot open input file 'QtDesignerd.lib'
这是因为此工程默认引用的是QtDesignerd.lib库,更改其为版本对应的库即可消除故障(VS2008是在项目的属性中Linker/input/Additional Dependencies中修改,我这里Debug配置使用的是QtDesignerd4.lib,Release版本使用QtDesigner4.lib)。
3、使用自定义插件
1)、
只需要把通过Release模式生成的 项目.lib和项目.dll文件拷到C:\Qt\4.7.4\plugins\designer中,
2)、 然后在QtDesigner中,选择菜单Help/About Plugin就可以看到你的自定义控件是否已经载入成功。
在QtDesigner中控件列表中有一项My Widget 中就有你的自定义控件。
参考:
打开QtDesigner,我们自定义的空间custom成功使用界面如下:
![](http://dl.iteye.com/upload/picture/pic/106978/6ed45d0e-42bc-3ef0-9822-c7d7e316de1f.jpg)