1. 程式人生 > >自定義控件時-添加多個控件到一組中

自定義控件時-添加多個控件到一組中

logs pub name 添加多個 tool 是否 imp efi 類名

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

自定義控件時-添加多個控件到一組中