Qt自定义控件之QSwitchButton

7 篇文章 0 订阅

模仿安卓/IOS的switch button,基于Qt,仅供大家学习参考
效果如下:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
代码如下:
QSwitchButton.h

#ifndef QSWITCHBUTTON_H
#define QSWITCHBUTTON_H

#include <QLabel>

class QSwitchButton : public QLabel
{
    Q_OBJECT

signals:
    /*
     * @brief       button点击信号
     * @param[in]   is_selected 当前是否选中
     */
    void clicked();
    void clicked(bool is_selected);

public:
    QSwitchButton(QWidget *parent);
    ~QSwitchButton();

    /*
     * @brief   按钮样式
     */
    typedef enum _buttonStyle{
        Rectage,    //矩形样式
        Ellipse,    //椭圆样式
    }ButtonStyle;

    /*
     * @brief   设置按钮样式
     */
    void SetButtonStyle(ButtonStyle button_style);

    /*
     * @brief       设置button的大小
     */
    void SetSize(int width, int height);
    void SetSize(const QSize& size);

    /*
     * @brief       设置背景颜色
     * @param[in]   selected_color      选中的颜色
     * @param[in]   not_selected_color  未选中的颜色
     */
    void SetBackgroundColor(const QColor& selected_color, const QColor& not_selected_color);

    /*
     * @brief       这是滑块颜色
     * @param[in]   selected_color      选中的颜色
     * @param[in]   not_selected_color  未选中的颜色
     */
    void SetSliderColor(const QColor& selected_color, const QColor& not_selected_color);

    /*
     * @brief       设置圆角弧度
     * @param[in]   round   设置的弧度
     */
    void SetRound(int round);

    /*
     * @brief       是否开启抗锯齿
     */
    void EnableAntiAliasing(bool enable);
    bool IsAntiAliasing();

    /*
     * @brief   当前是否是选中状态
     */
    void SetSelected(bool is_selected);
    bool IsSelected();

    /*
     * @brief   是否启用
     */
    void SetEnabel(bool is_enable);
    bool IsEnable();

protected:
    virtual void paintEvent(QPaintEvent* event);
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);

private:
    void DrawBackRect(QPainter* painter, const QRectF& rect);

    void DrawSliderRect(QPainter* painter, const QRectF& rect);

private:
    bool isEnable;  //是否启用

    bool isSelected;    //当前是否为选中状态
    bool isAntiAliasing;    //是否开启抗锯齿

    int buttonWidth;
    int buttonHeight;

    QColor backgroundColorSelected;
    QColor backgroundColorNotSelected;

    QColor sliderColorSelected;
    QColor sliderColorNotSelected;

    int rectRound;

    ButtonStyle buttonStyle;
};

#endif // QSWITCHBUTTON_H

QSwitchButton.cpp

#include "QSwitchButton.h"

#include <QPainter>
#include <QRectF>

QSwitchButton::QSwitchButton(QWidget *parent)
    : QLabel(parent), isSelected(true), buttonWidth(100), buttonHeight(40)
    , backgroundColorSelected(Qt::white), backgroundColorNotSelected("#E7E3E7")
    , sliderColorSelected("#63BAFF"), sliderColorNotSelected("#7B797B")
    , rectRound(30), isAntiAliasing(true), buttonStyle(Ellipse), isEnable(true)
{
    resize(buttonWidth, buttonHeight);
    setMouseTracking(true);
}

QSwitchButton::~QSwitchButton()
{

}

void QSwitchButton::SetButtonStyle(ButtonStyle button_style)
{
    buttonStyle = button_style;
    repaint();
}

void QSwitchButton::SetSize(int width, int height)
{
    buttonWidth = width;
    buttonHeight = height;

    resize(buttonWidth, buttonHeight);

    repaint();
}

void QSwitchButton::SetSize(const QSize& size)
{
    buttonWidth = size.width();
    buttonHeight = size.height();

    resize(buttonWidth, buttonHeight);

    repaint();
}

void QSwitchButton::SetBackgroundColor(const QColor& selected_color, const QColor& not_selected_color)
{
    backgroundColorSelected = selected_color;
    backgroundColorNotSelected = not_selected_color;

    repaint();
}

void QSwitchButton::SetSliderColor(const QColor& selected_color, const QColor& not_selected_color)
{
    sliderColorSelected = selected_color;
    sliderColorNotSelected = not_selected_color;

    repaint();
}

void QSwitchButton::SetRound(int round)
{
    rectRound = round;
    repaint();
}

void QSwitchButton::EnableAntiAliasing(bool enable)
{
    isAntiAliasing = enable;
    repaint();
}

bool QSwitchButton::IsAntiAliasing()
{
    return isAntiAliasing;
}

void QSwitchButton::SetSelected(bool is_selected)
{
    isSelected = is_selected;

    repaint();
}

bool QSwitchButton::IsSelected()
{
    return isSelected;
}

void QSwitchButton::SetEnabel(bool is_enable)
{
    isEnable = is_enable;

    repaint();
}

bool QSwitchButton::IsEnable()
{
    return isEnable;
}

void QSwitchButton::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);

    if (isAntiAliasing)
    {
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setRenderHint(QPainter::SmoothPixmapTransform);
    }

    if (!isEnable)  //未启用
    {
        painter.setPen(QPen(QColor("#F4F4F4")));
        painter.setBrush(QBrush(QColor("#F4F4F4")));
        DrawBackRect(&painter, QRectF(0, 0, buttonWidth, buttonHeight));

        painter.setPen(QPen(QColor("#ADB2B5")));
        painter.setBrush(QBrush(QColor("#ADB2B5")));
        DrawSliderRect(&painter, QRectF(buttonWidth*2/3-2, 2.5, buttonWidth/3, buttonHeight-5));
    }
    else if (isSelected)
    {
        painter.setPen(QPen(backgroundColorSelected));
        painter.setBrush(QBrush(backgroundColorSelected));
        DrawBackRect(&painter, QRectF(0, 0, buttonWidth, buttonHeight));

        painter.setPen(QPen(sliderColorSelected));
        painter.setBrush(QBrush(sliderColorSelected));
        DrawSliderRect(&painter, QRectF(buttonWidth*2/3-2, 2.5, buttonWidth/3, buttonHeight-5));
    }
    else
    {
        painter.setPen(QPen(backgroundColorNotSelected));
        painter.setBrush(QBrush(backgroundColorNotSelected));
        DrawBackRect(&painter,QRectF(0, 0, buttonWidth, buttonHeight));

        painter.setPen(QPen(sliderColorNotSelected));
        painter.setBrush(QBrush(sliderColorNotSelected));
        DrawSliderRect(&painter,QRectF(2, 2.5, buttonWidth/3, buttonHeight-5));
    }

    return QLabel::paintEvent(event);
}

void QSwitchButton::mousePressEvent(QMouseEvent *event)
{
    if (isEnable)
    {
        isSelected = !isSelected;
        repaint();

        emit clicked();
        emit clicked(isSelected);
    }

    return QLabel::mousePressEvent(event);
}

void QSwitchButton::mouseMoveEvent(QMouseEvent *event)
{
    setCursor(Qt::PointingHandCursor);

    return QLabel::mouseMoveEvent(event);
}

void QSwitchButton::DrawBackRect(QPainter* painter, const QRectF& rect)
{
    switch (buttonStyle)
    {
    case Rectage:
        painter->drawRoundRect(rect, rectRound, rectRound);
        break;
    case Ellipse:
        painter->drawEllipse(0, 0, buttonHeight, buttonHeight);
        painter->drawEllipse(buttonWidth - buttonHeight, 0, buttonHeight, buttonHeight);
        painter->drawRect(buttonHeight/2, 0, buttonWidth-buttonHeight, buttonHeight);
        break;
    default:
        break;
    }
}

void QSwitchButton::DrawSliderRect(QPainter* painter, const QRectF& rect)
{
    switch (buttonStyle)
    {
    case Rectage:
        painter->drawRoundRect(rect, rectRound, rectRound);
        break;
    case Ellipse:
        painter->drawEllipse(rect);
        break;
    default:
        break;
    }
}
Qt自定义控件大全Designer源码是一个包含了多种自定义控件的全套源码,它主要用于在Qt设计师中使用。Qt是一个跨平台的C++应用程序开发框架,提供了丰富的图形界面控件,但有时候我们可能需要自定义一些特殊的控件来满足我们的需求。 Qt自定义控件大全Designer源码包含了很多常用的自定义控件,如自定义按钮、进度条、滑块、验证码输入框等等。这些控件的设计和实现已经封装好,我们只需要将源码添加到我们的项目中,然后在Qt设计师中直接使用即可。 使用Qt自定义控件大全Designer源码有以下几个优点: 1.提供了丰富的自定义控件选择:Qt自定义控件大全Designer源码包含了多样化的控件,可以满足不同项目的需求。无论是一些简单的控件,还是一些复杂的控件,我们都可以找到合适的选择。 2.减少开发时间和工作量:使用源码中的自定义控件可以减少我们从头开始设计和实现的工作,节省了大量的开发时间和工作量。我们只需要将源码添加到项目中并正确配置,就可以直接在设计师中使用这些自定义控件。 3.提高应用程序的美观性和用户体验:Qt自定义控件大全Designer源码中的控件经过精心设计和实现,具有良好的界面效果和用户交互体验。使用这些自定义控件可以为我们的应用程序提供更加美观和友好的界面。 总之,Qt自定义控件大全Designer源码是一个提供了多种自定义控件的全套源码,使用它可以快速、方便地实现各种自定义控件,提高应用程序的开发效率和用户体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值