首页 > 解决方案 > 如何为 Qt Designer 集成自定义小部件(插件)

问题描述

我使用 Qt 的向导制作了一个 Qt 自定义设计器小部件。该插件在 Qt Designer 的插件文件夹中编译和安装没有问题,但它没有出现在 Qt 的 About Plugins 中。我使用QT_DEBUG_PLUGINS并注意到该插件不会出现在调试信息中。但是,当我编译另一个插件(不是 Qt 设计器的小部件)时,如SIGBUILD,它似乎已加载。为了集成这些插件,我不知道我在这里做错了什么。这是我用来制作插件的代码示例。有谁知道如何解决这个问题?

CustomLabel.pro

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

HEADERS     = CustomLabelPlugin.h
SOURCES     = CustomLabelPlugin.cpp
RESOURCES   = icons.qrc
DISTFILES   += $$PWD/CustomLabel.json

greaterThan(QT_MAJOR_VERSION, 4) {
    QT += designer
} else {
    CONFIG += designer
}

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

include(CustomLabel.pri)

自定义标签插件.h

#ifndef CUSTOMLABELPLUGIN_H
#define CUSTOMLABELPLUGIN_H

#include <QtUiPlugin/QDesignerCustomWidgetInterface>

class CustomLabelPlugin : public QObject, public QDesignerCustomWidgetInterface
{
    Q_OBJECT
    Q_INTERFACES(QDesignerCustomWidgetInterface)
#if QT_VERSION >= 0x050000
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "CustomLabel.json")
#endif // QT_VERSION >= 0x050000

public:
    CustomLabelPlugin(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 m_initialized;
};

#endif // CUSTOMLABELPLUGIN_H

自定义标签插件.cpp

#include "CustomLabel.h"
#include "CustomLabelPlugin.h"

#include <QtPlugin>

CustomLabelPlugin::CustomLabelPlugin(QObject *parent)
    : QObject(parent)
{
    m_initialized = false;
}

void CustomLabelPlugin::initialize(QDesignerFormEditorInterface * /* core */)
{
    if (m_initialized)
        return;

    // Add extension registrations, etc. here

    m_initialized = true;
}

bool CustomLabelPlugin::isInitialized() const
{
    return m_initialized;
}

QWidget *CustomLabelPlugin::createWidget(QWidget *parent)
{
    return new CustomLabel(parent);
}

QString CustomLabelPlugin::name() const
{
    return QLatin1String("CustomLabel");
}

QString CustomLabelPlugin::group() const
{
    return QLatin1String("ATRWidgets");
}

QIcon CustomLabelPlugin::icon() const
{
    return QIcon(QLatin1String(":/iconos/label.ico"));
}

QString CustomLabelPlugin::toolTip() const
{
    return QLatin1String("CustomLabel");
}

QString CustomLabelPlugin::whatsThis() const
{
    return QLatin1String("This is a custom QLabel...");
}

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

QString CustomLabelPlugin::domXml() const
{
    return QLatin1String("<widget class=\"ATRWidgets\" name=\"CustomLabel\">\n</widget>\n");
}

QString CustomLabelPlugin::includeFile() const
{
    return QLatin1String("CustomLabel.h");
}
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(CustomLabelPlugin, CustomLabelPlugin)
#endif // QT_VERSION < 0x050000

自定义标签.json

{
    "Name" : "CustomLabel",
    "Version" : "1.0.0",
    "CompatVersion" : "1.0.0",
    "Vendor" : "Asiel Torres",
    "Copyright" : "(C) Asiel Torres",
    "License" : "Evaluation license.",
    "Category" : "Qt Creator",
    "Description" : "This plugin presents a custom QLabel.",
    "Url" : ""
}

做一些测试,我发现 Qt 中有奇怪的工作原理。例如,如果我将插件从设计器文件夹移动到imageformats文件夹,那么 Qt 会检测到插件,然后插件的信息会出现在 Qt 调试中。根据 Qt 文档,放置在 Qt 搜索加载插件的任何文件夹中的插件应该可以工作。那么为什么信息会出现在一个插件文件夹的调试中而不出现在另一个文件夹中呢?无论如何,它会在调试中显示插件的信息,但 Qt 不会加载它。这是我从 Qt 调试信息中得到的信息:

QFactoryLoader::QFactoryLoader() checking directory path "/home/ragnarok/Qt5/5.15.0/gcc_64/plugins/imageformats" ...
QFactoryLoader::QFactoryLoader() looking at "/home/ragnarok/Qt5/5.15.0/gcc_64/plugins/imageformats/libCustomLabel.so"
Found metadata in lib /home/ragnarok/Qt5/5.15.0/gcc_64/plugins/imageformats/libCustomLabel.so, metadata=
{
    "IID": "org.qt-project.Qt.QDesignerCustomWidgetInterface",
    "archreq": 0,
    "className": "CustomLabelPlugin",
    "debug": false,
    "version": 331520
}


Got keys from plugin meta data ()

标签: qtqt-designerqtpluginqt5.15

解决方案


经过一番研究,我找到了解决方案并解决了问题。似乎 Designer 的插件为了工作必须放在文件夹QT_INSTALL_PATH/Tools/QtCreator/lib/Qt/plugins/designer/ 中,而不是像 Qt 的向导那样放在$$[QT_INSTALL_PLUGINS]/designer中。QT_INSTALL_PATH是安装 Qt 的文件夹为了确认 Qt 加载了插件,我们最常在 Qt 中打开一个表单编辑器,然后转到Tools/Form Editor/About Qt Designer Plugins...注意,我们必须在 Designer 中打开一个表单才能使该菜单正常工作。此菜单将显示所有已加载的插件,并允许我们刷新插件,以防我们想即时加载新插件。


推荐阅读