温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

QT自定义之滑动开关的实现方法

发布时间:2020-08-21 10:04:03 来源:亿速云 阅读:583 作者:小新 栏目:开发技术

这篇文章主要介绍了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自定义之滑动开关的实现方法内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

qt
AI