最近正在看《
C++ GUI
Qt4编程》,看到5.3在Qt设计师中集成自定义
窗口部件,我按照书上的方法好像不行。
首先使用改进法可以,
没有问题。
然后再尝试
插件法,结果在
编译时
出错了。代码如下:
iconeditorplugin.h文件内容
- #include <QDesignerCustomWidgetInterface>
- class IconEditorPlugin :public QObject, public QDesignerCustomWidgetInterface
- {
- Q_OBJECT
- Q_INTERFACES(QDesignerCustomWidgetInterface)
- public:
- IconEditorPlugin(QObject *parent = NULL);
- QString name() const;
- QString includeFile() const;
- QString group() const;
- QIcon icon() const;
- QString toolTip() const;
- QString whatsThis() const;
- bool isContainer() const;
- QWidget *createWidget(QWidget *parent);
- };
iconeditorplugin.cpp文件内容
- #include "iconeditorplugin.h"
- #include "iconeditor.h"
- IconEditorPlugin::IconEditorPlugin(QObject *parent)
- : QObject(parent)
- {
- }
- QString IconEditorPlugin::name() const
- {
- return "IconEditor";
- }
- QString IconEditorPlugin::includeFile() const
- {
- return "iconeditor.h";
- }
- QString IconEditorPlugin::group() const
- {
- return tr("Image Manipulation Widgets");
- }
- QIcon IconEditorPlugin::icon() const
- {
- return QIcon(":/images/iconeditor.png");
- }
- QString IconEditorPlugin::toolTip() const
- {
- return tr("An icon editor widget");
- }
- QString IconEditorPlugin::whatsThis() const
- {
- return tr("This widget is presented in Chapter 5 of <i>C++ GUI "
- "Programming with Qt 4</i> as an example of a custom Qt "
- "widget.");
- }
- bool IconEditorPlugin::isContainer() const
- {
- return false;
- }
- QWidget *IconEditorPlugin::createWidget(QWidget *parent)
- {
- return new IconEditor(parent);
- }
- Q_EXPORT_PLUGIN2(iconeditorplugin, IconEditorPlugin)
iconeditorplugin.pro文件内容
- TEMPLATE = lib
- CONFIG += designer plugin release
- HEADERS = iconeditor.h \
- iconeditorplugin.h
- SOURCES = iconeditor.cpp \
- iconeditorplugin.cpp
- RESOURCES = iconeditorplugin.qrc
- DESTDIR = $$[QT_INSTALL_PLUGINS]/designer
iconeditorplugin.qrc文件内容
- <RCC>
- <qresource>
- <file>images/iconeditor.png</file>
- </qresource>
- </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版本的。