QT自定义之滑动开关
更新时间:2020年8月21日 09:09 点击:1763
本文实例为大家分享了QT自定义之滑动开关的具体代码,供大家参考,具体内容如下
写了一个简单的滑动开关, 不多说,上图:
代码如下:
#ifndef SLIDERBUTTON_H #define SLIDERBUTTON_H #include <QWidget> #include <QMouseEvent> #include <QPaintEvent> #include <QPainter> #include <QPen> #include <QPainterPath> #include <QColor> #include <QTimer> #include <QDebug> namespace Ui { class SliderButton; } class SliderButton : public QWidget { Q_OBJECT public: explicit SliderButton(QWidget *parent = nullptr); ~SliderButton(); void set_button_size(const int &w, const int &h); void set_button_color(const QColor & , const QColor & ,const QColor & ); signals: void signal_button_on(); void signal_button_off(); protected: virtual void mousePressEvent(QMouseEvent *event); virtual void paintEvent(QPaintEvent *event); public slots: void slot_update(); private: bool m_button_status; int m_circle_width; int m_button_pos; int m_move_distance; QColor m_backcolor_on; QColor m_backcolor_off; QColor m_circle_color; QTimer *m_timer; }; #endif // SLIDERBUTTON_H
set_button_size可设置button大小。
set_button_color可设置button颜色
#include "sliderbutton.h" SliderButton::SliderButton(QWidget *parent) : QWidget (parent), m_button_status(false), m_circle_width(30), m_button_pos(0), m_move_distance(20), m_backcolor_on(Qt::red), m_backcolor_off(Qt::blue), m_circle_color(Qt::black) { setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(slot_update())); } SliderButton::~SliderButton() { } void SliderButton::set_button_size(const int & width, const int &heigh) { m_circle_width = heigh; m_move_distance = width; } void SliderButton::set_button_color(const QColor &on_color, const QColor &off_color, const QColor &button_color) { m_backcolor_on = on_color; m_backcolor_off = off_color; m_circle_color = button_color; } void SliderButton::mousePressEvent(QMouseEvent *event) { Q_UNUSED(event) if (false == m_button_status) { m_button_status = true; emit signal_button_off(); } else { m_button_status = false; emit signal_button_on(); } m_timer->start(1); } void SliderButton::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); QPainterPath path; painter.setRenderHint(QPainter::Antialiasing, true); if (m_button_status == true) { painter.setBrush(m_backcolor_off); } else { painter.setBrush(m_backcolor_on); } QRect rect (0, 0, m_circle_width, m_circle_width); int startX = rect.left() + rect.width() / 2; int startY = rect.top(); path.moveTo(startX,startY); path.arcTo(QRect(rect.left(), rect.top(), rect.width(), rect.height()),90,180); path.lineTo((rect.left() + m_move_distance ), rect.bottom() + 1); // the graph not connet , neet 1 pixcel path.arcTo(QRect((startX + m_move_distance),rect.top(),rect.width(),rect.height()),270,180); path.lineTo(startX,startY); painter.drawPath(path); // draw small circle painter.setBrush(m_circle_color); painter.drawEllipse(m_button_pos ,0,m_circle_width,m_circle_width); } void SliderButton::slot_update() { if (m_button_status == true) { m_button_pos += 1; if (m_button_pos == m_move_distance + m_circle_width / 2) { m_timer->stop(); } } else if(m_button_status == false) { m_button_pos -= 1; if (m_button_pos == 0) { m_timer->stop(); } } update(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: 使用QPainter画一个3D正方体
下一篇: Qt使用QPainter绘制3D立方体
相关文章
- 下面小编就为大家分享一篇Qt 使用Poppler实现pdf阅读器的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要介绍了将matplotlib绘图嵌入pyqt的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-27
- 在前一篇中我们介绍了键盘和鼠标事件,其实还有一个非常常用的事件,就是定时器事件,如果要对程序实现时间上的控制,那么就要使用到定时器。而随机数也是很常用的一个功能,在我们要想产生一个随机的结果时就要使用到随机数。本文我们就来简单介绍一下定时器和随机数。...2020-04-25
- 这篇文章主要给大家介绍了关于教你3分钟如何发布Qt程序的相关资料,文中通过实例代码结束的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-08-24
- 本文主要介绍了Qt使用windeployqt工具实现程序打包发布方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-11-01
- 这篇文章主要为大家详细介绍了Qt实现闹钟小程序,利用Qt的designer设计需要的闹钟界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 信号和槽是Qt特有的信息传输机制,本文主要介绍了浅谈Qt信号与槽的各种连接方式,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-02
- 这篇文章主要介绍了PyQt QMainWindow的使用示例,帮助大家更好的理解和学习使用pyqt,感兴趣的朋友可以了解下...2021-03-20
- 本文主要介绍了Qt实现UDP多线程数据处理及发送的简单实例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-21
- 这篇文章主要为大家详细介绍了Qt自定义控件实现线条型加载条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章主要为大家详细介绍了Qt自定义控件实现圆圈加载进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章主要为大家详细介绍了Qt实现Flappy Bird游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章主要为大家详细介绍了Qt使用QPainter绘制3D立方体,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-21
- 下面小编就为大家带来一篇QT开发应用程序的欢迎界面实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要为大家详细介绍了Qt实现进程界面之间的鼠标焦点切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-09-25
- 本文主要介绍了Qt实现网络聊天室,实现一个在线聊天室, 使用tcp对客户端和服务器端进行通讯。具有一定的参考价值,具有一定的参考价值,...2021-06-23
- 如果用qt写程序作为上位机,然后通过和usb和下位机通信的时候,就需要用到qt中的串口通信了。本文介绍了QT串口通信的实现方法,感兴趣的小伙伴们可以参考一下...2021-05-20
- 本文将介绍Qt之ui在程序中的使用-多继承法,需要的朋友可以参考...2020-04-25
- 下面小编就为大家带来一篇QT网络编程UDP下C/S架构广播通信(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要为大家详细介绍了jQuery实现滑动开关效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-02