c/c++开发分享QT自定义之滑动开关实例为大家分享了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(); }
以上就是c/c++开发分享QT自定义之滑动开关的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/597720.html