Qt自定义窗口部件

QtDesigner自定义窗口部件有两种方法:改进法(promotion)和插件法(plugin)

改进法


1、改进法之前,要先写好子类化QSpinBox后的HexspinBox.h和HexspinBox.cpp文件。把这两个文件拷贝到想要的项目中。

HexspinBox.h
Cpp代码 复制代码  收藏代码
  1. #ifndef HEXSPINBOX_H   
  2. #define HEXSPINBOX_H   
  3. #include <QSpinBox>   
  4. class QRegExpValidator;   
  5. class HexSpinBox : public QSpinBox   
  6. {   
  7.     Q_OBJECT   
  8.   
  9. public:   
  10.     HexSpinBox(QWidget *parent = 0);   
  11.   
  12. protected:   
  13.     QValidator::State validate(QString &text, int &pos) const;   
  14.     int valueFromText(const QString &text) const;   
  15.     QString textFromValue(int value) const;   
  16.   
  17. private:   
  18.     QRegExpValidator *validator;   
  19. };   
  20.   
  21. #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
Cpp代码 复制代码  收藏代码
  1. #include <QtGui>   
  2. #include "hexspinbox.h"   
  3. HexSpinBox::HexSpinBox(QWidget *parent)   
  4.     : QSpinBox(parent)   
  5. {   
  6.     setRange(0, 255);   
  7.     validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this);   
  8. }   
  9.   
  10. QValidator::State HexSpinBox::validate(QString &text, int &pos) const  
  11. {   
  12.     return validator->validate(text, pos);   
  13. }   
  14.   
  15. int HexSpinBox::valueFromText(const QString &text) const  
  16. {   
  17.     bool ok;   
  18.     return text.toInt(&ok, 16);   
  19. }   
  20.   
  21. QString HexSpinBox::textFromValue(int value) const  
  22. {   
  23.     return QString::number(value, 16).toUpper();   
  24. }  
#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源代码中可以知道,引入的控件是:
Cpp代码 复制代码  收藏代码
  1. #include <QtGui/QTableWidget>   
  2. #include <QtGui/QToolBar>   
  3. #include <QtGui/QWidget>   
  4. #include "hspinbox.h"   
  5.   
  6. QT_BEGIN_NAMESPACE   
  7.   
  8. class Ui_QMainClass   
  9. {   
  10. public:   
  11.     QWidget *centralWidget;   
  12.     QPushButton *pushButton;   
  13.     QTableWidget *tableWidget;   
  14.     QSpinBox *spinBox;   
  15.     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
源代码如下:

custom.h

 

Cpp代码 复制代码  收藏代码
  1. #ifndef CUSTOM_H   
  2. #define CUSTOM_H   
  3. #include <QtGui/QWidget>   
  4. #include "ui_test.h"   
  5. class custom : public QWidget   
  6. {   
  7.     Q_OBJECT   
  8. public:   
  9.     custom(QWidget *parent = 0);   
  10.     ~custom();   
  11. private:   
  12.     Ui::Form ui;   
  13. };   
  14.   
  15. #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

 

Cpp代码 复制代码  收藏代码
  1. #include "custom.h"   
  2.   
  3. custom::custom(QWidget *parent)   
  4.     : QWidget(parent)   
  5. {   
  6.     ui.setupUi(this);   
  7. }   
  8.   
  9. custom::~custom()   
  10. {   
  11.   
  12. }  
#include "custom.h"

custom::custom(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
}

custom::~custom()
{

}
 

customplugin.h

 

Cpp代码 复制代码  收藏代码
  1. #ifndef CUSTOMPLUGIN_H   
  2. #define CUSTOMPLUGIN_H   
  3.   
  4. #include <QDesignerCustomWidgetInterface>   
  5.   
  6. class customPlugin : public QObject, public QDesignerCustomWidgetInterface   
  7. {   
  8.     Q_OBJECT   
  9.     Q_INTERFACES(QDesignerCustomWidgetInterface)   
  10.   
  11. public:   
  12.     customPlugin(QObject *parent = 0);   
  13.   
  14.     bool isContainer() const;   
  15.     bool isInitialized() const;   
  16.     QIcon icon() const;   
  17.     QString domXml() const;   
  18.     QString group() const;   
  19.     QString includeFile() const;   
  20.     QString name() const;   
  21.     QString toolTip() const;   
  22.     QString whatsThis() const;   
  23.     QWidget *createWidget(QWidget *parent);   
  24.     void initialize(QDesignerFormEditorInterface *core);   
  25.   
  26. private:   
  27.     bool initialized;   
  28. };   
  29.   
  30. #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
Cpp代码 复制代码  收藏代码
  1. #include "custom.h"   
  2. #include <QtCore/QtPlugin>   
  3. #include "customplugin.h"   
  4. customPlugin::customPlugin(QObject *parent)   
  5.     : QObject(parent)   
  6. {   
  7.     initialized = false;   
  8. }   
  9.   
  10. void customPlugin::initialize(QDesignerFormEditorInterface */*core*/)   
  11. {   
  12.     if (initialized)   
  13.         return;   
  14.     initialized = true;   
  15. }   
  16.   
  17. bool customPlugin::isInitialized() const  
  18. {   
  19.     return initialized;   
  20. }   
  21.   
  22. QWidget *customPlugin::createWidget(QWidget *parent)   
  23. {   
  24.     return new custom(parent);   
  25. }   
  26.   
  27. QString customPlugin::name() const  
  28. {   
  29.     return "custom";   
  30. }   
  31.   
  32. QString customPlugin::group() const  
  33. {   
  34.     return "My Plugins";   
  35. }   
  36.   
  37. QIcon customPlugin::icon() const  
  38. {   
  39.     return QIcon();   
  40. }   
  41.   
  42. QString customPlugin::toolTip() const  
  43. {   
  44.     return QString();   
  45. }   
  46.   
  47. QString customPlugin::whatsThis() const  
  48. {   
  49.     return QString();   
  50. }   
  51.   
  52. bool customPlugin::isContainer() const  
  53. {   
  54.     return false;   
  55. }   
  56.   
  57. QString customPlugin::domXml() const  
  58. {   
  59.     return "<widget class=\"custom\" name=\"custom\">\n"  
  60.         " <property name=\"geometry\">\n"  
  61.         "  <rect>\n"  
  62.         "   <x>0</x>\n"  
  63.         "   <y>0</y>\n"  
  64.         "   <width>100</width>\n"  
  65.         "   <height>100</height>\n"  
  66.         "  </rect>\n"  
  67.         " </property>\n"  
  68.         "</widget>\n";   
  69. }   
  70.   
  71. QString customPlugin::includeFile() const  
  72. {   
  73.     return "custom.h";   
  74. }   
  75.   
  76. 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的最后必须 添加下面的宏:

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. 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成功使用界面如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值