一,首先新建一个lib工程,

qt自定义控件_#if

例如:spprogressbar

  spprogressbar.pro

CONFIG      += plugin debug_and_release
TARGET      = $$qtLibraryTarget(spprogressbarplugin)
TEMPLATE    = lib
 
DESTDIR     = $$PWD/
 
HEADERS     += \
            $$PWD/spprogressbarplugin.h\
            $$PWD/spprogressbar.h \
 
SOURCES     += \
            $$PWD/spprogressbarplugin.cpp\
            $$PWD/spprogressbar.cpp \
 
 
LIBS        += -L.
 
greaterThan(QT_MAJOR_VERSION, 4) {
    QT += designer
} else {
    CONFIG += designer
}
 
target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS    += target
 
INCLUDEPATH += $$HEADER_PATH/
 

  

  spprogressbarplugin.h

#ifndef SPHOMEICONPLUGIN_H
#define SPHOMEICONPLUGIN_H

#include <QDesignerCustomWidgetInterface>

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

public:
    SpProgressBarPlugin(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

 

  spprogressbarplugin.cpp

#include "spprogressbar.h"
#include "spprogressbarplugin.h"

#include <QtPlugin>

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

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

    // Add extension registrations, etc. here

    m_initialized = true;
}

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

QWidget *SpProgressBarPlugin::createWidget(QWidget *parent)
{
    return new SpProgressBar(parent);
}

QString SpProgressBarPlugin::name() const
{
    return QLatin1String("SpProgressBar");
}

QString SpProgressBarPlugin::group() const
{
    return QLatin1String("Sunplus Widgets");
}

QIcon SpProgressBarPlugin::icon() const
{
    return QIcon();
}

QString SpProgressBarPlugin::toolTip() const
{
    return QLatin1String("SpProgressBar");
}

QString SpProgressBarPlugin::whatsThis() const
{
    return QLatin1String("this is a progress bar.");
}

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

QString SpProgressBarPlugin::domXml() const
{
    return QLatin1String("<widget class=\"SpProgressBar\" name=\"spProgressBar\">\n</widget>\n");
}

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

 

  spprogressbar.h

#ifndef SPPROGRESSBAR_H
#define SPPROGRESSBAR_H

#include <QWidget>
#include <QPainter>
#include <QMouseEvent>

class SpProgressBar : public QWidget
{
    Q_OBJECT

public:
    SpProgressBar(QWidget *parent = 0);
};

#endif

  spprogressbar.cpp

#include "spprogressbar.h"

SpProgressBar::SpProgressBar(QWidget *parent) :
    QWidget(parent)
{

}

 

二,然后将编译生成的dll文件libspprogressbarplugin.dll放到Qt\Qt5.9.1\mingw53_32\plugins\designer下

 

三,重启designer就可以看到自动以的控件了