How to integrate custom widget (plugin) for Qt Designer

1.5k Views Asked by At

I made a Qt Custom Designer Widget using Qt's Wizard. The plugin compile and install on the Qt Designer's plugin folder without problems but it does not appear loaded in the About Plugins of Qt. I use the QT_DEBUG_PLUGINS and notice that the plugin dosen't appear in the debug info. However when I compile another plugin (which is not a widget for the Qt Designer) like the SIGBUILD it appears loaded. I don't know what I'm doing wrong here in order to integrate these kind of plugins. Here is an example of the code I use to make the plugins. Does anyone have ideas how to solve this problem?

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)

CustomLabelPlugin.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

CustomLabelPlugin.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" : ""
}

Doing some tests I discovered strange workings in Qt. For example if I move the plugin from the designer folder to the imageformats folder then Qt detects the plugin and then plugin's information appears in the Qt debug. According to the Qt documentation the plugin placed in any folder in which Qt searches to load plugins should work. So why does information appear in the debug in one plugin folder and not in another? Anyway the plugin's info it shows up in the debug but Qt doesn't load it. Here is what i got from Qt debug info:

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 ()
1

There are 1 best solutions below

0
On BEST ANSWER

After a litle more research I found the solution and fixed the problem. It seems that Designer's Plugins in order to work must be placed in the folder QT_INSTALL_PATH/Tools/QtCreator/lib/Qt/plugins/designer/ and not in $$[QT_INSTALL_PLUGINS]/designer as the Qt's wizard do. Being QT_INSTALL_PATH the folder where Qt is installed. In order to confirm that Qt loaded the plugins we most open a form editor inside Qt en go to Tools/Form Editor/About Qt Designer Plugins... Note we must have a form open in the Designer in order to this menu to work. This menu will show up all the plugins loaded and allow us to Refresh the plugins in case we want to load a new plugins on the fly.