Qt实现苹果状态切换按钮

时间:2022-08-26 19:32:45

本文实例为大家分享了Qt实现苹果状态切换按钮的具体代码,供大家参考,具体内容如下

Qt实现苹果状态切换按钮

代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "button.h"
#include <QDebug>
 
Button::Button(QWidget *parent) :
 QPushButton(parent)
{
 status = 0;
 span_length = 0;
 rl_flag = false;
 release_flag =true;
 timeline = new QTimeLine;
 connect(timeline,SIGNAL(frameChanged(int)),this,SLOT(timeOut(int)));
}
 
void Button::paintEvent(QPaintEvent *e)
{
 QPainter painter(this);
 painter.setRenderHint(QPainter::Antialiasing, true);
 QPen pen;
 pen.setWidth(2);
 pen.setColor(QColor(219,219,219));
 painter.setPen(pen);
 painter.setBrush(Qt::green);
 painter.drawRoundedRect(1,1,this->width()-2,this->height()-2,30,30);
 
 painter.setBrush(Qt::white);
 painter.setPen(pen);
 
 if(!rl_flag){
  switch(status)
  {
  case 0:painter.drawEllipse(1,1,this->height()-3,this->height()-3);break;
  case 1:painter.drawRoundedRect(1,1,span_length,this->height()-2,30,30);break;
  case 2:painter.drawEllipse(this->width()+3-this->height(),1,this->height()-3,this->height()-3);rl_flag=true;status=0;break;
  }
 }else{
  switch(status)
  {
  case 0:painter.drawEllipse(this->width()+3-this->height(),1,this->height()-3,this->height()-3);break;
  case 1:painter.drawRoundedRect(this->width()-1-span_length,1,span_length,this->height()-2,30,30);break;
  case 2:painter.drawEllipse(1,1,this->height()-3,this->height()-3);rl_flag=false;status=0;break;
  }
 }
}
 
void Button::drawSpan(QPainter painter,int s)
{
 
}
 
void Button::mousePressEvent(QMouseEvent *e)
{
 timeline->setDuration(400);
 span_length = 0;
 status=1;
 if(!rl_flag){
  int r1 = this->height()-3;
  int r2 = (this->width()/4*3)>r1?(this->width()/4*3):r1;
  timeline->setFrameRange(r1,r2);
 }else{
  int r1 = this->height()-3;
  int r2 = (this->width()/4*3)>r1?(this->width()/4*3):r1;//(this->width()/4*1)>r1?(this->width()/4*1):r1;
  timeline->setFrameRange(r1,r2);
 }
 timeline->start();
}
 
void Button::mouseMoveEvent(QMouseEvent *e)
{
}
 
void Button::mouseReleaseEvent(QMouseEvent *e)
{
 timeline->stop();
 status = 2;
 update();
}
 
void Button::timeOut(int x)
{
 status = 1;
 span_length = x;
 update();
}

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

原文链接:https://blog.csdn.net/gaobobo138968/article/details/46300775