QT自定义之滑动开关

所属分类: 软件编程 / C 语言 阅读数: 80
收藏 0 赞 0 分享

本文实例为大家分享了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++无法重载点符号、::、sizeof等的原因

这篇文章主要介绍了C++无法重载点符号、::、sizeof等的原因的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

实例讲解C语言编程中的结构体对齐

这篇文章主要介绍了C语言编程中的结构体对齐,值得注意的是一些结构体对齐的例子在不同编译器下结果可能会不同,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言的结构体中成员变量偏移问题

这篇文章主要介绍了C语言的结构体中成员变量偏移问题,以讲解如何编写宏来对成员变量进行修改为主,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言结构体中的函数指针

这篇文章主要介绍了详解C语言结构体中的函数指针,文中对函数指针的基本概念也有讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

C++编程中的函数指针初步解析

这篇文章主要介绍了C++编程中的函数指针初步解析,函数指针在C语言和C++学习中都是非常重要的知识,需要的朋友可以参考下
收藏 0 赞 0 分享

实例解析C++中类的成员函数指针

这篇文章主要介绍了C++中类的成员函数指针,例子中以讨论用函数指针调用类的成员函数为主,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的函数指针基础学习教程

这篇文章主要介绍了C语言中的函数指针基础学习教程,包括函数指针作为参数来传递等重要知识,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解析C语言中函数指针的定义与使用

这篇文章主要介绍了C语言中函数指针的定义与使用,是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

详解C语言编程中的函数指针以及函数回调

这篇文章主要介绍了C语言编程中的函数指针以及函数回调,函数回调实际上就是让函数指针作函数参数、调用时传入函数地址,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的函数指针学习笔记

这篇文章主要介绍了C语言中的函数指针的一些学习知识点记录,文中作者整理了一些比较interesting的函数指针用法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多