• qt自定义控件


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

    例如: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">
    </widget>
    ");
    }
    
    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放到QtQt5.9.1mingw53_32pluginsdesigner下

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

    四,源码地址链接: https://pan.baidu.com/s/1AnhZkBxt8hNQGDVmc9F7ZA 密码: ****

      联系作者可获得密码,QQ:820977192

        

  • 相关阅读:
    JDBC 基本语法总结
    MySQL 基本语法
    mysql学习第三天练习(日期和时间函数)
    mysql学习第三天练习(流程控制函数)
    mysql学习第三天笔记
    mysql学习第二天函数
    mysql学习第一天select
    mysql学习第一天
    Java石头剪刀布小游戏
    Java万年历
  • 原文地址:https://www.cnblogs.com/senior-engineer/p/9603867.html
Copyright © 2020-2023  润新知