Qt自定义控件之QSwitchButton

模仿安卓/IOS的switch button,基于Qt,仅供大家学习参考
效果如下:
Qt自定义控件之QSwitchButton_第1张图片
这里写图片描述
这里写图片描述
这里写图片描述
代码如下:
QSwitchButton.h

#ifndef QSWITCHBUTTON_H
#define QSWITCHBUTTON_H

#include 

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 
#include 

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,C/C++)