首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法构建多部件QtDesigner插件

无法构建多部件QtDesigner插件
EN

Stack Overflow用户
提问于 2014-03-13 19:33:33
回答 1查看 741关注 0票数 2

我试图创建一个单一的DLL "CustomPlugins“,其中包含两个QtDesigner插件。通过复制WorldClockPlugin示例,我能够让每个插件单独工作。但是,当我试图将两者都包含在单个DLL中时,我会得到以下生成错误:

代码语言:javascript
复制
LNK2005: _qt_plugin_instance already defined in moc_qledplugin.obj

在我看来,通过使插件包装小部件都继承QDesignerCustomWidgetInterface,这将导致_qt_plugin_instance函数的多个定义。我查看了moc文件和定制小部件头文件(由QDesignerCustomWidgetInterface包含),但找不到对此函数的任何引用。

QDesignerExportWidget包含在两个插件头中,QDESIGNER_WIDGET_EXPORT包含在两个小部件声明中。

难道就不可能将两个插件封装在同一个DLL中吗?还是我漏掉了什么?

谢谢。

下面是代码。qdragabletoolboxplugin.*文件(未显示)与qledplugin*文件几乎相同。此外,qdragabletoolbox (也未显示)是以与qled相同的方式声明的,但显然会做一些非常不同的事情。为了节省空间,我省略了这些文件,用省略号替换了一些函数定义。

.pro文件:

代码语言:javascript
复制
QT          += widgets designer

QMAKE_LFLAGS += /INCREMENTAL:NO

TARGET      = $$qtLibraryTarget($$TARGET)
CONFIG     += plugin
TEMPLATE    = lib

target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += target

HEADERS     = qled.h \
              qledplugin.h \
              qdragabletoolbox.h \
              qdragabletoolboxplugin.h
SOURCES     = qled.cpp \
              qledplugin.cpp \
              qdragabletoolbox.cpp \
              qdragabletoolboxplugin.cpp

RESOURCES += CustomDesignerPlugins.qrc

qled.h:

代码语言:javascript
复制
#ifndef QLED_H
#define QLED_H

#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QList>
#include <QColor>
#include <QGradient>
#include <QtDesigner/QDesignerExportWidget>

class QDESIGNER_WIDGET_EXPORT QLED : public QWidget
{
    Q_OBJECT

    Q_ENUMS(ledColor)
    Q_ENUMS(ledShape)

    Q_PROPERTY(ledColor offColor READ offColor WRITE setOffColor)
    Q_PROPERTY(ledColor onColor READ onColor WRITE setOnColor)
    Q_PROPERTY(bool shadow READ shadow WRITE setShadow)
    Q_PROPERTY(ledShape shape READ shape WRITE setShape)
    Q_PROPERTY(bool value READ value WRITE setValue)

public:
    enum ledColor { Red = 0, Orange, Yellow, Green, Blue, Indigo, Violet, Black, Gray, DarkGray };
    enum ledShape { Ellipse = 0, Rectangle, Circle, Square };

    explicit QLED(QWidget *parent = NULL);
    virtual ~QLED() { /*empty*/ }

    bool value() const { return mValue; }
    ledColor onColor() const { return mOnColor; }
    ledColor offColor() const { return mOffColor; }
    ledShape shape() const { return mShape; }
    bool shadow() const { return mShadow; }

signals:

public slots:
    void setValue(bool value) { mValue = value; update(); }
    void setShape(ledShape shape) { mShape = shape; update(); }
    void setOnColor(ledColor color) { mOnColor = color; update(); }
    void setOffColor(ledColor color) { mOffColor = color; update(); }
    void setShadow(bool b) { mShadow = b; update(); }
    void toggle() { mValue = !mValue; update(); }

protected:
    void paintEvent(QPaintEvent *event);

private:
    void drawBezel(QPainter *painter);
    void drawFace(QPainter *painter);
    void drawShadow(QPainter *painter);

private:
    bool mValue;

    ledColor mOnColor;
    ledColor mOffColor;

    bool mBezel;
    float  mBezelWidth;
    ledColor mBezelColor;

    bool mShadow;

    ledShape mShape;

    QList<QColor> colors;
};

#endif

qled.cpp

代码语言:javascript
复制
#include "qled.h"

QLED::QLED(QWidget *parent)
    : QWidget(parent)
{
    ...
}

void QLED::paintEvent(QPaintEvent *event)
{
    ...
}

void QLED::drawBezel(QPainter *painter)
{
    ...
}

void QLED::drawFace(QPainter *painter)
{
    ...
}

void QLED::drawShadow(QPainter *painter)
{
    ...
}

qledplugin.h

代码语言:javascript
复制
#ifndef QLEDPLUGIN_H
#define QLEDPLUGIN_H

#include <QDesignerCustomWidgetInterface>

class QLEDPlugin : public QObject, public QDesignerCustomWidgetInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "qled.json")
    Q_INTERFACES(QDesignerCustomWidgetInterface)

public:
    QLEDPlugin(QObject *parent = 0);

    bool isContainer() const;
    bool isInitialized() const;
    QIcon icon() const;
    QString domXml() const;
    QString group() const;
    QString includeFile() const;
    QString name() const;
    QString toolTip() const;
    QString whatsThis() const;
    QWidget *createWidget(QWidget *parent);
    void initialize(QDesignerFormEditorInterface *core);

private:
    bool initialized;
};

#endif

qledplugin.cpp

代码语言:javascript
复制
#include "qled.h"
#include "qledplugin.h"

#include <QtPlugin>

QLEDPlugin::QLEDPlugin(QObject *parent)
    : QObject(parent)
{
    initialized = false;
}

void QLEDPlugin::initialize(QDesignerFormEditorInterface *core)
{
    Q_UNUSED(core);
    if (initialized)
        return;
    else
        initialized = true;
}

bool QLEDPlugin::isInitialized() const
{
    return initialized;
}

QWidget *QLEDPlugin::createWidget(QWidget *parent)
{
    return new QLED(parent);
}

QString QLEDPlugin::name() const
{
    return QLatin1String("QLED");
}

QString QLEDPlugin::group() const
{
    return QLatin1String("Display Widgets");
}

QIcon QLEDPlugin::icon() const
{
    return QIcon(QLatin1String(":/QLED/LEDIcon.png"));
}

QString QLEDPlugin::toolTip() const
{
    return "Binary status indicator";
}

QString QLEDPlugin::whatsThis() const
{
    return "";
}

bool QLEDPlugin::isContainer() const
{
    return false;
}

QString QLEDPlugin::domXml() const
{
    return "<ui language=\"c++\">\n"
            " <widget class=\"QLED\" name=\"qLed\">\n"
            "  <property name=\"geometry\">\n"
            "   <rect>\n"
            "    <x>0</x>\n"
            "    <y>0</y>\n"
            "    <width>25</width>\n"
            "    <height>25</height>\n"
            "   </rect>\n"
            "  </property>\n"
            " </widget>\n"
            "</ui>";;
}

QString QLEDPlugin::includeFile() const
{
    return QLatin1String("QtWidgets/QLED.h");
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-13 20:02:34

编译器错误消息提供了所需的所有信息:您有两个小部件,但只需要一个插件。

而不是实现QDesignerCustomWidgetInterface,而是实现QDesignerCustomWidgetCollectionInterface

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22389067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档