繁体   English   中英

如何为 Qt Designer 集成自定义小部件(插件)

[英]How to integrate custom widget (plugin) for Qt Designer

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

自定义标签.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

CustomLabel.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 中有奇怪的工作方式。例如,如果我将插件从designer文件夹移动到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 ()

经过更多研究后,我找到了解决方案并解决了问题。 为了工作,Designer 的插件似乎必须放在文件夹QT_INSTALL_PATH/Tools/QtCreator/lib/Qt/plugins/designer/中,而不是像 Qt 向导那样放在$$[QT_INSTALL_PLUGINS]/designer中。 作为QT_INSTALL_PATH安装 Qt 的文件夹。 为了确认 Qt 加载了插件,我们最常在 Qt en go 中打开一个表单编辑器到工具/表单编辑器/关于 Qt 设计器插件...请注意,我们必须在设计器中打开一个表单才能使用此菜单。 这个菜单将显示所有加载的插件,并允许我们刷新插件,以防我们想要动态加载新插件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM