• 5121阅读
  • 2回复

[提问]请问如何制作Designer自定义部件? [复制链接]

上一主题 下一主题
离线wosuopu
 

只看楼主 倒序阅读 楼主  发表于: 2011-12-05
关键词: QT4Designer
最近正在看《C++ GUI Qt4编程》,看到5.3在Qt设计师中集成自定义窗口部件,我按照书上的方法好像不行。


首先使用改进法可以,没有问题


然后再尝试插件法,结果在编译出错了。代码如下:
iconeditorplugin.h文件内容
  1. #include <QDesignerCustomWidgetInterface>
  2. class IconEditorPlugin :public QObject, public QDesignerCustomWidgetInterface
  3. {
  4.     Q_OBJECT
  5.     Q_INTERFACES(QDesignerCustomWidgetInterface)
  6. public:
  7.     IconEditorPlugin(QObject *parent = NULL);
  8.     QString name() const;
  9.     QString includeFile() const;
  10.     QString group() const;
  11.     QIcon icon() const;
  12.     QString toolTip() const;
  13.     QString whatsThis() const;
  14.     bool isContainer() const;
  15.     QWidget *createWidget(QWidget *parent);
  16. };
iconeditorplugin.cpp文件内容
  1. #include "iconeditorplugin.h"
  2. #include "iconeditor.h"
  3. IconEditorPlugin::IconEditorPlugin(QObject *parent)  
  4.     : QObject(parent)  
  5. {  
  6. }  
  7. QString IconEditorPlugin::name() const  
  8. {  
  9.     return "IconEditor";  
  10. }  
  11. QString IconEditorPlugin::includeFile() const  
  12. {  
  13.     return "iconeditor.h";  
  14. }  
  15. QString IconEditorPlugin::group() const  
  16. {  
  17.     return tr("Image Manipulation Widgets");  
  18. }  
  19. QIcon IconEditorPlugin::icon() const  
  20. {  
  21.     return QIcon(":/images/iconeditor.png");  
  22. }  
  23. QString IconEditorPlugin::toolTip() const  
  24. {  
  25.     return tr("An icon editor widget");  
  26. }  
  27. QString IconEditorPlugin::whatsThis() const  
  28. {  
  29.     return tr("This widget is presented in Chapter 5 of <i>C++ GUI "  
  30.               "Programming with Qt 4</i> as an example of a custom Qt "  
  31.               "widget.");  
  32. }  
  33. bool IconEditorPlugin::isContainer() const  
  34. {  
  35.     return false;  
  36. }  
  37. QWidget *IconEditorPlugin::createWidget(QWidget *parent)  
  38. {  
  39.     return new IconEditor(parent);  
  40. }  
  41. Q_EXPORT_PLUGIN2(iconeditorplugin, IconEditorPlugin)
iconeditorplugin.pro文件内容
  1. TEMPLATE        = lib
  2. CONFIG         += designer plugin release
  3. HEADERS         = iconeditor.h \
  4.                   iconeditorplugin.h
  5. SOURCES         = iconeditor.cpp \
  6.                   iconeditorplugin.cpp
  7. RESOURCES       = iconeditorplugin.qrc
  8. DESTDIR         = $$[QT_INSTALL_PLUGINS]/designer
iconeditorplugin.qrc文件内容
  1. <RCC>
  2. <qresource>
  3.     <file>images/iconeditor.png</file>
  4. </qresource>
  5. </RCC>

结果编译时出现错误提示:

iconeditorplugin.cpp:42:17: 错误:expected constructor, destructor, or type conversion before ‘(’ token
make: *** [iconeditorplugin.o] 错误 1

这用到的iconeditor.h、iconeditor.cpp文件是这本书前一节用到的。
为什么会这样呢?我用的是Qt 4.7.4版本的。
离线roywillow

只看该作者 1楼 发表于: 2011-12-05
我只能说……这东西写起来似乎真的挺需要人品的……

错误提示在哪行?
专业维修核潜艇,回收二手航母、二手航天飞机,大修核反应堆,拆洗导弹发动机更换机油,无人侦察机手动挡改自动,航天飞机保养换三滤,飞碟外太空年检 ,各型号导弹加装迎宾踏板,高空作业擦洗卫星表面除尘、打蜡及抛光,东风全系列巡航导弹。并提供原子对撞机。量大从优,有正规发票。
离线wosuopu

只看该作者 2楼 发表于: 2011-12-05
错误是iconeditorplugin.cpp文件的42行,就是这句代码:

Q_EXPORT_PLUGIN2(iconeditorplugin, IconEditorPlugin)
快速回复
限100 字节
 
上一个 下一个