c++ - 无法构建多部件 QtDesigner 插件

标签 c++ qt qt-designer lnk2005

我正在尝试创建一个包含两个 QtDesigner 插件的 DLL“CustomPlugins”。通过复制 WorldClockPlugin 示例,我能够让每个插件单独工作。然而,当我尝试将它们都包含在一个 DLL 中时,我收到以下构建错误:

LNK2005: _qt_plugin_instance already defined in moc_qledplugin.obj

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

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

是否根本不可能将两个插件包装在同一个 DLL 中?或者我错过了什么?

谢谢。

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

.pro 文件:

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:

#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

#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

#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

#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");
}

最佳答案

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

不要实现QDesignerCustomWidgetInterface,而是实现QDesignerCustomWidgetCollectionInterface

关于c++ - 无法构建多部件 QtDesigner 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389067/

相关文章:

c++ - OpenCV 3.0 无法加载神经网络

c++ - QString 相对于 std::string 的优势

python - 文件对话框不适用于 PyQt5?

c++ - Qt Creator 在运行时不显示 .ui 更改

c++ - 为什么 QueryInterface() 会在接口(interface)确实实现并且在 Windows 中具有内置编码器时失败?

c++ - 转换嵌套包的每个内包

c++ - 使用 libcurl 和 C++ 将图片上传到受密码保护的目录

c++ - 在 QT 中启动多个外部控制台应用程序实例并捕获输出?

python - QWebView 不加载外部 CSS

python - QtDesigner 预览与实际 GUI 不同