dmwidgetinterface.h
 1 #ifndef _DM_WIDGET_INTERFACE_H_
 2 #define _DM_WIDGET_INTERFACE_H_
 3 
 4 
 5 #include <QDesignerCustomWidgetInterface>
 6 
 7 
 8 // DM自定义控件
 9 class DmWidgetInterface : public QObject, public QDesignerCustomWidgetInterface
10 {
11     Q_OBJECT
12     Q_INTERFACES(QDesignerCustomWidgetInterface)
13 
14 public:
15     DmWidgetInterface(const QString& className, QObject* parent = 0);
16     virtual ~DmWidgetInterface();
17 
18     // 控件在Designer中显示的图标
19     virtual QIcon icon() const;
20 
21     // 控件在Designer中显示的名称
22     virtual QString name() const;
23 
24     // 控件在Designer中所在的组名称
25     virtual QString group() const;
26 
27     // 控件在.ui文件中的默认描述
28     virtual QString domXml() const;
29 
30     // 控件在Designer中鼠标悬浮时的提示信息
31     virtual QString toolTip() const;
32     virtual QString whatsThis() const;
33 
34     // 控件是否是一个容器类控件,可以包容其他控件
35     virtual bool isContainer() const;
36 
37 private:
38     // 类名
39     QString className;
40 };
41 
42 
43 // 生成控件接口类
44 #define DECLARE_WIDGET_INTERFACE(WidgetName) \
45 \
46 class WidgetName##Interface : public DmWidgetInterface \
47 { \
48     Q_INTERFACES(QDesignerCustomWidgetInterface) \
49 \
50 public: \
51     WidgetName##Interface(QObject* parent = 0); \
52     virtual ~WidgetName##Interface(); \
53 \
54     virtual QString includeFile() const; \
55 \
56     virtual QWidget* createWidget(QWidget* parent); \
57 };
58 
59 
60 // 实现控件接口类
61 #define IMPLEMENT_WIDGET_INTERFACE(WidgetName, IncludeFile) \
62 \
63 WidgetName##Interface::WidgetName##Interface(QObject* parent) : DmWidgetInterface(#WidgetName, parent) \
64 { \
65 \
66 } \
67 \
68 WidgetName##Interface::~WidgetName##Interface() \
69 { \
70     \
71 } \
72 \
73 QString WidgetName##Interface::includeFile() const \
74 { \
75     return (IncludeFile); \
76 } \
77 \
78 QWidget* WidgetName##Interface::createWidget(QWidget* parent) \
79 { \
80     return new WidgetName(parent); \
81 }
82 
83 
84 #endif // _DM_WIDGET_INTERFACE_H_
View Code
dmwidgetinterface.cpp
 1 #include "dmwidgetinterface.h"
 2 
 3 
 4 DmWidgetInterface::DmWidgetInterface(const QString& name, QObject* parent)
 5     : QObject(parent), className(name)
 6 {
 7 
 8 }
 9 
10 DmWidgetInterface::~DmWidgetInterface()
11 {
12 
13 }
14 
15 // 控件在Designer中显示的图标
16 QIcon DmWidgetInterface::icon() const
17 {
18     return QIcon();
19 }
20 
21 // 控件在Designer中显示的名称
22 QString DmWidgetInterface::name() const
23 {
24     return this->className;
25 }
26 
27 // 控件在Designer中所在的组名称
28 QString DmWidgetInterface::group() const
29 {
30     return "DmWidgets";
31 }
32 
33 // 控件在.ui文件中的默认描述
34 QString DmWidgetInterface::domXml() const
35 {
36     return QDesignerCustomWidgetInterface::domXml();
37 }
38 
39 // 控件在Designer中鼠标悬浮时的提示信息
40 QString DmWidgetInterface::toolTip() const
41 {
42     return "";
43 }
44 
45 QString DmWidgetInterface::whatsThis() const
46 {
47     return "";
48 }
49 
50 // 控件是否是一个容器类控件,可以包容其他控件
51 bool DmWidgetInterface::isContainer() const
52 {
53     return false;
54 }
View Code
#ifndef _DM_WIDGET_INTERFACES_H_
#define _DM_WIDGET_INTERFACES_H_

#ifndef QT5VERSION
#include <QDesignerCustomWidgetInterface>
#else
#include "QtUiPlugin/customwidget.h"
#endif
// DM自定义控件
class DmWidgetInterfaces : public QObject, public QDesignerCustomWidgetCollectionInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetCollectionInterface")	   	   
    Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)

public:
    DmWidgetInterfaces(QObject* parent = 0);
    virtual ~DmWidgetInterfaces();

    // 控件列表
    virtual QList<QDesignerCustomWidgetInterface*> customWidgets() const;

private:
    // 控件列表
    QList<QDesignerCustomWidgetInterface*> widgets;
};


#endif // _DM_WIDGET_INTERFACES_H_

  

dmwidgetinterfaces.cpp
#include <QtCore/QtPlugin>


#include "dmtexteditinterface.h"


DmWidgetInterfaces::DmWidgetInterfaces(QObject* parent)
    : QObject(parent)
{
    // 多行文本输入
    this->widgets.append(new DmTextEditInterface(this));

}

DmWidgetInterfaces::~DmWidgetInterfaces()
{

}

// 控件列表
QList<QDesignerCustomWidgetInterface*> DmWidgetInterfaces::customWidgets() const
{
    return this->widgets;
}


Q_EXPORT_PLUGIN(DmWidgetInterfaces)
View Code
dmtextedit.h
#ifndef _DM_TEXT_EDIT_H
#define _DM_TEXT_EDIT_H

#include <QtGui/QTextEdit>

#include "libwidgets.h"

// 多行输入文本控件
class WIDGETS_EXPORT DmTextEdit : public QTextEdit
{
    Q_OBJECT 

public:
    DmTextEdit(QWidget* parent = 0);
    virtual ~DmTextEdit();
 
};
#endif
View Code
dmtexteditinterface.cpp
1 #include "dmtextedit.h"
2 #include "dmtexteditinterface.h"
3 
4 
5 // 多行输入文本控件
6 IMPLEMENT_WIDGET_INTERFACE(DmTextEdit, "dmtextedit.h")
View Code
dmtexteditinterface.h
 1 #ifndef _DM_TEXTEDIT_INTERFACE_H_
 2 #define _DM_TEXTEDIT_INTERFACE_H_
 3 
 4 
 5 #include "dmwidgetinterface.h"
 6 
 7 // 多行输入的文本控件
 8 DECLARE_WIDGET_INTERFACE(DmTextEdit)
 9 
10 
11 #endif
View Code

 

posted on 2017-05-20 16:33  卡贝天师  阅读(623)  评论(0编辑  收藏  举报