簡體   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