Qt自定义控件之QSwitchButton

来源:互联网 发布:网络引论 编辑:程序博客网 时间:2024/06/05 02:12

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

#ifndef QSWITCHBUTTON_H#define QSWITCHBUTTON_H#include <QLabel>class QSwitchButton : public QLabel{    Q_OBJECTsignals:    /*     * @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;    }}
原创粉丝点击