Qt之等待提示框二(纯代码实现)

#include "progress_indicator.h"

QProgressIndicator::QProgressIndicator(QWidget* parent)
 : QWidget(parent)
{
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    setFocusPolicy(Qt::NoFocus);

 angle = 0,
 timer_id = -1,
 delay_time = 40,
 is_display_when_stop = false,
 color = Qt::black;
}

bool QProgressIndicator::isAnimated () const
{
    return (timer_id != -1);
}

int QProgressIndicator::animationDelay() const
{
 return delay_time;
}

void QProgressIndicator::setColor(const QColor &color)
{
 this->color = color;
 update();
}

QColor & QProgressIndicator::getColor()
{
 return color;
}

void QProgressIndicator::setDisplayWhenStop(bool is_display_when_stop)
{
    this->is_display_when_stop = is_display_when_stop;
    update();
}

bool QProgressIndicator::isDisplayWhenStop() const
{
    return is_display_when_stop;
}

void QProgressIndicator::startAnimation()
{
    angle = 0;
    if(timer_id == -1)
 {
  timer_id = startTimer(delay_time);
 }  
}

void QProgressIndicator::stopAnimation()
{
    if(timer_id != -1)
 {
  killTimer(timer_id);
 }
    timer_id = -1;
    update();
}

void QProgressIndicator::setAnimationDelay(int delay_time)
{
    if(timer_id != -1)
 {
  killTimer(timer_id);
 }
       
    this->delay_time = delay_time;

    if(timer_id != -1)
 {
  timer_id = startTimer(delay_time);
 }  
}

void QProgressIndicator::timerEvent(QTimerEvent *)
{
    angle = (angle + 30) % 360;
    update();
}

void QProgressIndicator::paintEvent(QPaintEvent *)
{
    if(!is_display_when_stop && !isAnimated())
        return;

    int width = qMin(this->width(), this->height());
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
   
    int outer_radius = (width - 1) * 0.5;
    int inner_radius = (width - 1) * 0.5 * 0.4;

    int capsule_height = outer_radius - inner_radius;
    int capsule_width  = (width > 32 ) ? capsule_height *.30 : capsule_height *.35;
    int capsule_radius = capsule_width/2;

    for(int i=0; i<12; i++)
    {
        QColor color = color;
        color.setAlphaF(1.0f - (i/12.0f));
        p.setPen(Qt::NoPen);
        p.setBrush(color);      
        p.save();
        p.translate(rect().center());
        p.rotate(angle - i*30.0f);
        p.drawRoundedRect(-capsule_width*0.5, -(inner_radius+capsule_height), capsule_width, capsule_height, capsule_radius, capsule_radius);
        p.restore();
    }
}

 

  效果图:

Qt之等待提示框二(纯代码实现)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值