QT自定义之滑动开关

 更新时间:2020年08月20日 14:05:24   作者:mario_z  
这篇文章主要为大家详细介绍了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++选择排序算法实例

    这篇文章主要介绍了C++选择排序算法实例,本文先是介绍了什么是选择排序,然后给出了实现代码,需要的朋友可以参考下
    2014-10-10
  • C语言中结构体偏移及结构体成员变量访问方式的问题讨论

    C语言中结构体偏移及结构体成员变量访问方式的问题讨论

    这篇文章主要介绍了C语言中结构体偏移及结构体成员变量访问方式的问题讨论,帮助大家理解struct的成员变量偏移,需要的朋友可以参考下
    2016-05-05
  • C++ Assert()断言机制原理以及使用方法

    C++ Assert()断言机制原理以及使用方法

    下面小编就为大家带来一篇C++ Assert()断言机制原理以及使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • C++17结构化绑定的实现

    C++17结构化绑定的实现

    这篇文章主要介绍了C++17结构化绑定的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Qt自定义控件实现简易仪表盘

    Qt自定义控件实现简易仪表盘

    这篇文章主要为大家详细介绍了Qt自定义控件实现简易仪表盘,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • Qt实现苹果状态切换按钮

    Qt实现苹果状态切换按钮

    这篇文章主要为大家详细介绍了Qt实现苹果状态切换按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • 打印菱形以及斐波纳契数列的几种解法介绍

    打印菱形以及斐波纳契数列的几种解法介绍

    本篇文章是对打印菱形及斐波纳契数列的几种解法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C语言用fun函数实现两个数的交换方式

    C语言用fun函数实现两个数的交换方式

    这篇文章主要介绍了C语言用fun函数实现两个数的交换方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • opencv C++模板匹配的简单实现

    opencv C++模板匹配的简单实现

    这篇文章主要介绍了opencv C++模板匹配的简单实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • C语言动态内存管理malloc柔性数组示例详解

    C语言动态内存管理malloc柔性数组示例详解

    这篇文章主要为大家介绍了C语言动态内存管理malloc柔性数组示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10

最新评论