直播平台搭建源码,qt自定义滑动按钮

直播平台搭建源码,qt自定义滑动按钮

代码:

 

switchbutton.h

 

 

#ifndef switchbutton_H
#define switchbutton_H
#include <QWidget>
#include <QTimer>
#include <QColor>
class switchbutton : public QWidget
{
    Q_OBJECT
public:
    explicit switchbutton(QWidget *parent = 0);
    ~switchbutton(){}
signals:
    void statusChanged(bool checked);
public slots:
    void updateValue();
private:
    void drawBackGround(QPainter *painter);
    void drawSlider(QPainter *painter);
protected:
    void paintEvent(QPaintEvent *ev);
    void mousePressEvent(QMouseEvent *ev);
private:
    int m_space;                //滑块距离边界距离
    int m_radius;               //圆角角度
    bool m_checked;             //是否选中
    bool m_showText;            //是否显示文字
    bool m_showCircle;          //是否显示圆圈
    bool m_animation;           //是否使用动画
    QColor m_bgColorOn;         //打开时候的背景色
    QColor m_bgColorOff;        //关闭时候的背景色
    QColor m_sliderColorOn;     //打开时候滑块颜色
    QColor m_sliderColorOff;    //关闭时候滑块颜色
    QColor m_textColor;         //文字颜色
    QString m_textOn;           //打开时候的文字
    QString m_textOff;          //关闭时候的文字
    QTimer  *m_timer;            //动画定时器
    int     m_step;             //动画步长
    int     m_startX;           //滑块开始X轴坐标
    int     m_endX;             //滑块结束X轴坐标
public:
    int space()                 const;
    int radius()                const;
    bool checked()              const;
    bool showText()             const;
    bool showCircel()           const;
    bool animation()            const;
    QColor bgColorOn()          const;
    QColor bgColorOff()         const;
    QColor sliderColorOn()      const;
    QColor sliderColorOff()     const;
    QColor textColor()          const;
    QString textOn()            const;
    QString textOff()           const;
    int step()                  const;
    int startX()                const;
    int endX()                  const;
public Q_SLOTS:
    void setSpace(int space);
    void setRadius(int radius);
    void setChecked(bool checked);
    void setShowText(bool show);
    void setShowCircle(bool show);
    void setAnimation(bool ok);
    void setBgColorOn(const QColor &color);
    void setBgColorOff(const QColor &color);
    void setSliderColorOn(const QColor &color);
    void setSliderColorOff(const QColor &color);
    void setTextColor(const QColor &color);
    void setTextOn(const QString &text);
    void setTextOff(const QString &text);
};
#endif // switchbutton_H
 

switchbutton.cpp

 


#pragma execution_character_set("utf-8")
#include "switchbutton.h"
#include <QPainter>
switchbutton::switchbutton(QWidget *parent) : QWidget(parent)
{
    m_space = 2;
    m_radius = 5;
    m_checked = false;
    m_showText = true;
    m_showText = false;
    m_animation = true;
    m_bgColorOn = QColor(102, 205, 170);//设置按钮内部颜色
    m_bgColorOff = QColor(190, 190, 190);
    m_sliderColorOn = QColor(255, 255, 255);
    m_sliderColorOff = QColor(255, 255, 255);
    m_textColor = QColor(255, 255, 255);
    m_textOn = "开启";//设置按钮中的文字
    m_textOff = "关闭";
    m_step = 0;
    m_startX = 0;
    m_endX = 0;
    m_timer = new QTimer(this);
    m_timer->setInterval(30);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(updateValue()));
}
void switchbutton::drawBackGround(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    QColor bgColor = m_checked ? m_bgColorOn : m_bgColorOff;
    if (isEnabled()) {
        bgColor.setAlpha(150);
    }
    painter->setBrush(bgColor);
    QRect rect(0, 0, width(), height());
    int side = qMin(width(), height());
    //左侧半圆
    QPainterPath path1;
    path1.addEllipse(rect.x(), rect.y(), side, side);
    //右侧半圆
    QPainterPath path2;
    path2.addEllipse(rect.width() - side, rect.y(), side, side);
    //中间的矩形
    QPainterPath path3;
    path3.addRect(rect.x() + side / 2, rect.y(), rect.width() - side, height());
    QPainterPath path = path1 + path2 + path3;
    painter->drawPath(path);
    //绘制文本
    //滑块半径
    int sliderWidth = qMin(height(), width()) - m_space * 2 - 5;
    if (m_checked){
        QRect textRect(0, 0, width() - sliderWidth, height());
        painter->setPen(QPen(m_textColor));
        painter->drawText(textRect, Qt::AlignCenter, m_textOn);
    } else {
        QRect textRect(sliderWidth, 0, width() - sliderWidth, height());
        painter->setPen(QPen(m_textColor));
        painter->drawText(textRect, Qt::AlignCenter, m_textOff);
    }
    painter->restore();
}

 

 以上就是直播平台搭建源码,qt自定义滑动按钮, 更多内容欢迎关注之后的文章

 

posted @ 2023-03-30 14:08  云豹科技-苏凌霄  阅读(46)  评论(0)    收藏  举报