码迷,mamicode.com
首页 > 其他好文 > 详细

Qt开关按钮

时间:2020-01-19 19:02:43      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:sse   map   margin   als   type   protected   ons   oid   void   

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();
    void clicked(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 EnableAntiAliasing(bool enable);
    bool IsAntiAliasing();

    //当前是否是选中状态
    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;    //当前是否为选中状态
    bool isAntiAliasing;    //是否开启抗锯齿

    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),
    isSelected(true), buttonWidth(100), buttonHeight(40),
    backgroundColorSelected("#52C300"), backgroundColorNotSelected("#6B797B"),
    sliderColorSelected("#FEFBEF"), sliderColorNotSelected("#F7F5F7"),
   rectRound(10), isAntiAliasing(true), buttonStyle(Rectage), isEnable(true)

{
    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::EnableAntiAliasing(bool enable)
{
    isAntiAliasing = enable;
    repaint();
}

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

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);

    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+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();
        emit clicked(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;
    }
}

 

Qt开关按钮

标签:sse   map   margin   als   type   protected   ons   oid   void   

原文地址:https://www.cnblogs.com/ike_li/p/12215000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!