Qt开关按钮

1.

 

#ifndef SWITCHBUTTON_H
#define SWITCHBUTTON_H

#include <QObject>
#include <QLabel>
class SwitchButton : public QLabel
{
    typedef enum _buttonStyle{
      Rectage,    //矩形样式
      Ellipse,    //椭圆样式
    }ButtonStyle;

    Q_OBJECT
public:
    explicit SwitchButton(QWidget  *parent = nullptr);
    ~SwitchButton();
signals: 
    void sigClicked(bool is_selected);
public slots:
public:
    void setButtonStyle(ButtonStyle button_style);
    void setSize(int width, int height);
    void setSize(const QSize& size);
    void setBackgroundColor(const QColor& selected_color, const QColor& not_selected_color);
    void setSliderColor(const QColor& selected_color, const QColor& not_selected_color);
    void setRound(int round);

    //当前是否是选中状态
    void setSelected(bool is_selected);
    bool isSelected();

    //是否启用
    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_;    //当前是否为选中状态
    int buttonWidth;
    int buttonHeight;
    QColor backgroundColorSelected;
    QColor backgroundColorNotSelected;
    QColor sliderColorSelected;
    QColor sliderColorNotSelected;
    int rectRound;
    ButtonStyle buttonStyle;

};

#endif // SWITCHBUTTON_H
#include "switchbutton.h"

#include <QPainter>
//调整位置
const int X_MARGIN=5;
const int Y_MARGIN=5;
SwitchButton::SwitchButton(QWidget  *parent) :
    QLabel(parent),
    isEnable_(true),
    isSelected_(true),
    buttonWidth(100),
    buttonHeight(40),
    backgroundColorSelected("#52C300"),
    backgroundColorNotSelected("#6B797B"),
    sliderColorSelected("#FEFBEF"),
    sliderColorNotSelected("#F7F5F7"),
    rectRound(10),
    buttonStyle(Rectage)

{
    resize(buttonWidth, buttonHeight);
    setMouseTracking(true);
}

SwitchButton::~SwitchButton()
{

}
void SwitchButton::setButtonStyle(ButtonStyle button_style)
{
    buttonStyle = button_style;
    repaint();
}

void SwitchButton::setSize(int width, int height)
{
    buttonWidth = width;
    buttonHeight = height;

    resize(buttonWidth, buttonHeight);

    repaint();
}

void SwitchButton::setSize(const QSize& size)
{
    buttonWidth = size.width()+X_MARGIN;
    buttonHeight = size.height()+Y_MARGIN;

    resize(buttonWidth, buttonHeight);

    repaint();
}

void SwitchButton::setBackgroundColor(const QColor& selected_color, const QColor& not_selected_color)
{
    backgroundColorSelected = selected_color;
    backgroundColorNotSelected = not_selected_color;

    repaint();
}

void SwitchButton::setSliderColor(const QColor& selected_color, const QColor& not_selected_color)
{
    sliderColorSelected = selected_color;
    sliderColorNotSelected = not_selected_color;

    repaint();
}

void SwitchButton::setRound(int round)
{
    rectRound = round;
    repaint();
}

void SwitchButton::setSelected(bool is_selected)
{
    isSelected_ = is_selected;
    repaint();
}

bool SwitchButton::isSelected()
{
    return isSelected_;
}

void SwitchButton::setEnabel(bool is_enable)
{
    isEnable_ = is_enable;

    repaint();
}

bool SwitchButton::isEnable()
{
    return isEnable_;
}

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

    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+X_MARGIN, 2.5+Y_MARGIN, buttonWidth/3, buttonHeight-5));
    }
    else if (isSelected_)
    {
        painter.setPen(QPen(backgroundColorSelected));
        painter.setBrush(QBrush(backgroundColorSelected));
        drawBackRect(&painter, QRectF(0+X_MARGIN, 0+Y_MARGIN, buttonWidth, buttonHeight));

        painter.setPen(QPen(sliderColorSelected));
        painter.setBrush(QBrush(sliderColorSelected));
        drawSliderRect(&painter, QRectF(buttonWidth*2/3-2+X_MARGIN, 2.5+Y_MARGIN, buttonWidth/3, buttonHeight-5));

        QFont font;
        //font.setBold(true);
        font.setPointSize(10);
        painter.setFont(font);
        painter.setPen(Qt::white);
        painter.drawText(QRectF(X_MARGIN+10, Y_MARGIN+5, buttonWidth, buttonHeight),QStringLiteral("CW"));

    }
    else
    {
        painter.setPen(QPen(backgroundColorNotSelected));
        painter.setBrush(QBrush(backgroundColorNotSelected));
        drawBackRect(&painter,QRectF(X_MARGIN, Y_MARGIN, buttonWidth, buttonHeight));

        painter.setPen(QPen(sliderColorNotSelected));
        painter.setBrush(QBrush(sliderColorNotSelected));
        drawSliderRect(&painter,QRectF(2+X_MARGIN, 2.5+Y_MARGIN, buttonWidth/3, buttonHeight-5));

        QFont font;
        //font.setBold(true);
        font.setPointSize(10);
        painter.setFont(font);
        painter.setPen(Qt::white);
        painter.drawText(QRectF(buttonWidth/2+5,Y_MARGIN+5, buttonWidth, buttonHeight),QStringLiteral("CCW"));
    }

    return QLabel::paintEvent(event);
}

void SwitchButton::mousePressEvent(QMouseEvent *event)
{
    if (isEnable_)
    {
        isSelected_ = !isSelected_;
        repaint();


        emit sigClicked(isSelected_);
    }

    return QLabel::mousePressEvent(event);
}

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

    return QLabel::mouseMoveEvent(event);
}

void SwitchButton::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 SwitchButton::drawSliderRect(QPainter* painter, const QRectF& rect)
{
    switch (buttonStyle)
    {
        case Rectage:
            painter->drawRoundRect(rect, rectRound, rectRound);
            break;
        case Ellipse:
            painter->drawEllipse(rect);
            break;
        default:
            break;
    }
}
原文地址:https://www.cnblogs.com/ike_li/p/12215000.html