欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

QT自定义之滑动开关

程序员文章站 2022-08-21 15:57:59
本文实例为大家分享了qt自定义之滑动开关的具体代码,供大家参考,具体内容如下写了一个简单的滑动开关, 不多说,上图:代码如下:#ifndef sliderbutton_h#define sliderb...

本文实例为大家分享了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();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: QT 滑动开关