Qt自定义控件

  • 开发系统:ubuntu22.04
  • IDE:clion
  • 构建工具:cmake

Qt自定义控件之插件形式

插件形式是指将自定义控件按照一定的规则,生成动态库,放到Qt designer插件加载目录/usr/lib/x86_64-linux-gnu/qt5/plugins/designer下,Qt designer启动时加载,自定义控件就像内置控件一样可以直接拖拽。这样可以做到自定义控件全局可用。
下面是效果图:

自定义控件依赖QDesignerCustomWidgetInterface类,使用此类需要安装qttools5-dev

sudo apt install qttools5-dev

一个简单示例

下面以一个自定义的Label来介绍,为了简单起见,这个Label相对于QLabel只是可以设置默认的字体颜色(原本的QLabel就可以)。
首先写一个如下cmake,用于动态库的编译

cmake_minimum_required(VERSION 3.28)
project(MyLabel)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5 COMPONENTS
        Core
        Gui
        Widgets
        Designer
        REQUIRED)

include_directories(${PROJECT_SOURCE_DIR})

aux_source_directory(${PROJECT_SOURCE_DIR} SRCS)
file(GLOB QRCS "*.qrc")

add_library(MyLabel SHARED
        ${SRCS}
        ${QRCS})

target_link_libraries(MyLabel
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        Qt5::Designer)

定义MyLabel类: mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class MyLabel : public QLabel
{
    Q_OBJECT
    Q_PROPERTY(QColor color READ textColor WRITE setTextColor)

public:
    MyLabel(QWidget *parent = nullptr);
    ~MyLabel();
    void setTextColor(const QColor &c);
    QColor textColor() const;

private:
    QColor color;
};

#endif //MYLABEL_H

mylabel.cpp

#include "mylabel.h"

MyLabel::MyLabel(QWidget *parent)
    : QLabel(parent)
{
    color = QColor(Qt::black);
    setPalette(QPalette(color));
}

MyLabel::~MyLabel()
{
}

void MyLabel::setTextColor(const QColor &c)
{
    color = c;
    setPalette(QPalette(color));
}

QColor MyLabel::textColor() const
{
    return color;
}

定义MyLabel 插件类:mylabelplugin.h

#ifndef MYLABELPLUGIN_H
#define MYLABELPLUGIN_H

#include <QtUiPlugin/QDesignerCustomWidgetInterface>

class MyLabelPlugin : public QObject, public QDesignerCustomWidgetInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID QDesignerCustomWidgetInterface_iid)
    Q_INTERFACES(QDesignerCustomWidgetInterface)

public:
    MyLabelPlugin(QObject *parent = nullptr);
    QString name() const;
    QString includeFile() const;
    QString group() const;
    QIcon icon() const;
    QString toolTip() const;
    QString whatsThis() const;
    bool isContainer() const;
    QWidget *createWidget(QWidget *parent);
};

#endif //MYLABELPLUGIN_H

mylabelplugin.cpp

#include "mylabelplugin.h"

#include "mylabel.h"

MyLabelPlugin::MyLabelPlugin(QObject *parent)
{
}

QString MyLabelPlugin::name() const
{
    return "MyLabel";
}

QString MyLabelPlugin::includeFile() const
{
    return "mylabel.h";
}

QString MyLabelPlugin::group() const
{
    return tr("Custom Widgets");
}

QIcon MyLabelPlugin::icon() const
{
    return QIcon(":/images/mylabel.png");
}

QString MyLabelPlugin::toolTip() const
{
    return tr("An custom label widget");
}

QString MyLabelPlugin::whatsThis() const
{
    return tr("balabala balabala.");
}

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

QWidget *MyLabelPlugin::createWidget(QWidget *parent)
{
    return new MyLabel(parent);
}

还未解决头文件包含问题,库的链接问题

posted @ 2024-07-26 18:31  thammer  阅读(3)  评论(0编辑  收藏  举报