Qt实现苹果状态切换按钮
程序员文章站
2022-07-04 11:19:29
本文实例为大家分享了qt实现苹果状态切换按钮的具体代码,供大家参考,具体内容如下代码如下#include "button.h"#include button::butto...
本文实例为大家分享了qt实现苹果状态切换按钮的具体代码,供大家参考,具体内容如下
代码如下
#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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。