zoukankan      html  css  js  c++  java
  • 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  
     
     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. }  
     
    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;  
     
       
    升级法的缺点是不能在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  
     

     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. }  
     

    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  
     
     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"> "  
    60.         " <property name="geometry"> "  
    61.         "  <rect> "  
    62.         "   <x>0</x> "  
    63.         "   <y>0</y> "  
    64.         "   <width>100</width> "  
    65.         "   <height>100</height> "  
    66.         "  </rect> "  
    67.         " </property> "  
    68.         "</widget> ";  
    69. }  
    70.   
    71. QString customPlugin::includeFile() const  
    72. {  
    73.     return "custom.h";  
    74. }  
    75.   
    76. 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:Qt4.7.4pluginsdesigner中,
        2)、 然后在QtDesigner中,选择菜单Help/About Plugin就可以看到你的自定义控件是否已经载入成功。
    在QtDesigner中控件列表中有一项My Widget 中就有你的自定义控件。
     
    参考:
     
     
     
    打开QtDesigner,我们自定义的空间custom成功使用界面如下:
     
     
     
  • 相关阅读:
    第二十二篇 正在表达式 re模块
    机器学习 —— 概率图模型(推理:采样算法)
    机器学习 —— 概率图模型(推理:MAP)
    机器学习 —— 概率图模型(推理:团树算法)
    机器学习 —— 概率图模型(推理:消息传递算法)
    第二次读书会的思考
    机器学习 —— 概率图模型(推理:变量消除)
    机器学习 —— 概率图模型(马尔科夫与条件随机场)
    机器学习 —— 概率图模型(CPD)
    机器学习 —— 概率图模型(贝叶斯网络)
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/3758086.html
Copyright © 2011-2022 走看看