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

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

代码:

 

switchbutton.h

 

 

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

switchbutton.cpp

 

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

 

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

 

posted @   云豹科技-苏凌霄  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2022-03-30 在线直播系统源码,指定字体呈现高亮效果
2022-03-30 手机直播源码,每日定时刷新用户任务
2022-03-30 短视频app制作,附近功能通过用户IP地址确定位置
点击右上角即可分享
微信分享提示