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

改进法

 
1、改进法之前,要先写好子类化QSpinBox后的HexspinBox.h和HexspinBox.cpp文件。把这两个文件拷贝到想要的项目中。
 
HexspinBox.h
  1. #ifndef HEXSPINBOX_H
  2. #define HEXSPINBOX_H
  3. #include <QSpinBox>
  4. class QRegExpValidator;
  5. class HexSpinBox : public QSpinBox
  6. {
  7. Q_OBJECT
  8. public:
  9. HexSpinBox(QWidget *parent = 0);
  10. protected:
  11. QValidator::State validate(QString &text, int &pos) const;
  12. int valueFromText(const QString &text) const;
  13. QString textFromValue(int value) const;
  14. private:
  15. QRegExpValidator *validator;
  16. };
  17. #endif
 
 HexspinBox.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. QValidator::State HexSpinBox::validate(QString &text, int &pos) const
  10. {
  11. return validator->validate(text, pos);
  12. }
  13. int HexSpinBox::valueFromText(const QString &text) const
  14. {
  15. bool ok;
  16. return text.toInt(&ok, 16);
  17. }
  18. QString HexSpinBox::textFromValue(int value) const
  19. {
  20. return QString::number(value, 16).toUpper();
  21. }
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源代码中可以知道,引入的控件是:
  1. #include <QtGui/QTableWidget>
  2. #include <QtGui/QToolBar>
  3. #include <QtGui/QWidget>
  4. #include "hspinbox.h"
  5. QT_BEGIN_NAMESPACE
  6. class Ui_QMainClass
  7. {
  8. public:
  9. QWidget *centralWidget;
  10. QPushButton *pushButton;
  11. QTableWidget *tableWidget;
  12. QSpinBox *spinBox;
  13. HSpinBox *hspinBox;
   
升级法的缺点是不能在Qt Designer中设置自定义控件自己的特有属性,也不能够绘制自己。这些问题可以用插件法解决。

插件法

 
1.VS中创建Qt4 Design Plugin 工程 ,名称叫custom
 
自动建立如下几个文件:
自定义控件:custom.h,custom.cpp
插件:customplugin.h,customplugin.cpp
源代码如下:

custom.h

  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. #endif // CUSTOM_H

custom.cpp

  1. #include "custom.h"
  2. custom::custom(QWidget *parent)
  3. : QWidget(parent)
  4. {
  5. ui.setupUi(this);
  6. }
  7. custom::~custom()
  8. {
  9. }

customplugin.h

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

Qt自定义窗口部件的更多相关文章

  1. Qt入门(19)——自定义窗口部件

    我们介绍可以画自己的第一个自定义窗口部件.我们也加入了一个有用的键盘接口.我们添加了一个槽:setRange().        void setRange( int minVal, int maxV ...

  2. C++ GUI Qt4编程-创建自定义窗口部件

    C++ GUI Qt4编程-创建自定义窗口部件   Qtqt4 通过Qt窗口部件进行子类化或者直接对QWidget进行子类化,就可以创建自定义窗口部件,下面示范两种方式,并且也会说明如何把自定义窗口部 ...

  3. C++框架_之Qt的窗口部件系统的详解-上

    C++框架_之Qt的窗口部件系统的详解-上 第一部分概述 第一次建立helloworld程序时,曾看到Qt Creator提供的默认基类只有QMainWindow.QWidget和QDialog三种. ...

  4. Kivy 中文教程 实例入门 简易画板 (Simple Paint App):1. 自定义窗口部件 (widget)

    1. 框架代码 用 PyCharm 新建一个名为 SimplePaintApp 的项目,然后新建一个名为 simple_paint_app.py 的 Python 源文件, 在代码编辑器中,输入以下框 ...

  5. Qt学习之自定义窗口部件

    自定义Qt窗口部件 实现一个十六进制的SpinBox,一般SpinBox只支持十进制整数,但是可以子类化方法实现该功能 需重新实现以下虚函数 virtual QString textFromValue ...

  6. QT自定义窗口(模拟MainWindow)

    在这里自定义窗口是通过继承于QFrame,内部分为上下两部分,上半部分就是标题栏,下面是窗口的具体内容.上下两部分通过布局固定位置.最后窗口的各种鼠标拖动,缩放,等操作通过添加鼠标事件来完成一个窗口的 ...

  7. QT自定义窗口

    qt 中允许自定义窗口控件,使之满足特殊要求, (1)可以修改其显示,自行绘制 (2)可以动态显示 (3)可以添加事件,支持鼠标和键盘操作 自定义控件可以直接在QtDesigner里使用,可以直接加到 ...

  8. QT5中如何自定义窗口部件

    提升法 eg.(定义一个新的QLable部件)1.定义一个类class Label : public base, public QLabel //可以支持多重继承2.在qt creator中打开ui编 ...

  9. qt 自定义窗口绘制正弦曲线

    circlewidget.h #ifndef CIRCLAWIDGET_H #define CIRCLAWIDGET_H #include <QFrame> #include<QVe ...

随机推荐

  1. webapi 之 post参数传递

    最近在写webapi,在写post请求接口时遇到了不少的问题,在此记录下来. post请求的参数和get请求有点不一样,我们知道get请求的参数是通过url来传递的,而post请求则是通过http的请 ...

  2. Jmeter服务器压力测试使用说明

    Jmeter服务器压力测试使用说明 Apache JMeter是Apache组织开发的基于Java的压力测试工具. 官方地址:http://jmeter.apache.org/download_jme ...

  3. BootStrap【一、概述】

    4月底出去浪了一圈,回来收了一周的心才收回来,5.12,重启自学 今天早上总算大概把JAVASpring相关的东西过了一边,Spring基础.IOC.AOP,看的有些头晕脑胀 公司项目除了Spring ...

  4. shell脚本——sed命令

    sed 命令 作为行编辑器,对文件进行编辑(以行为单位进行编辑) sed编辑文件,却不改变原文件 sed工作原理: 指定一个文本文件,依次读取文本文件中的每行内容,读取到模式空间中,在模式空间中进行匹 ...

  5. Junit5常用注解

    0. IDEA中Maven项目测试类的新建方法 a. 如图在src目录下新建文件夹test b. 鼠标右键test,将该文件设置成test source c. 右键需要新建的测试类,如下图操作,选中T ...

  6. gitbook 准备一 [python3 WSGI 初探]

    目录 1.wsgi服务样例 2.请求样例 1.wsgi服务样例 # 官网样例 from wsgiref.util import setup_testing_defaults from wsgiref. ...

  7. Hadoop_29_MapReduce_计数器应用

    在实际生产代码中,常常需要将数据处理过程中遇到的不合规数据行进行全局计数,类似这种需求可以借助mapreduce框架中 提供的全局计数器来实现 示例代码如下: public class MultiOu ...

  8. Nexus Repository Manager OSS 2 配置阿里云私服做代理的坑

    安装 搭建 Nexus 私服很简单,官网下载,解压: 使用管理员权限打开cmd: > cd nexus---bundle\nexus--\bin > nexus.bat install # ...

  9. 使用ADB命令写Android自动化测试脚本

    使用脚本来执行测试的特点: ●书写方便 ●基本上可以实现90%以上的功能性覆盖 ●测试结果需要通过自己观察整个过程和日志文件来得出的 ●有些外部的动作,脚本是无法实现的,比如录入指纹 ●只适配特定尺寸 ...

  10. Qt 之 qInstallMessageHandler(日志重定向至文件)

    Qt 日志重定向到文件 #include <QCoreApplication> #include <QDebug> #include <QMutex> #inclu ...