Qt之等待提示框(QProgressIndicator)

简述

很早以前在网上看到一个纯代码实现的旋转动画感觉效果很不错,分享给大家。不得不说,条条大道通罗马,我们需要更多地创造。。。

详见:QProgressIndicator

  • 简述
  • 效果
  • 源码
  • 使用

效果

由于录制程序的原因,引起gif效果不清晰,可忽略。

源码

QProgressIndicator.h

#ifndef QPROGRESSINDICATOR_H
#define QPROGRESSINDICATOR_H

#include <QWidget>
#include <QColor>

/*!
    \class QProgressIndicator
    \brief The QProgressIndicator class lets an application display a progress indicator to show that a lengthy task is under way. 

    Progress indicators are indeterminate and do nothing more than spin to show that the application is busy.
    \sa QProgressBar
*/
class QProgressIndicator : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int delay READ animationDelay WRITE setAnimationDelay)
    Q_PROPERTY(bool displayedWhenStopped READ isDisplayedWhenStopped WRITE setDisplayedWhenStopped)
    Q_PROPERTY(QColor color READ color WRITE setColor)
public:
    QProgressIndicator(QWidget* parent = 0);

    /*! Returns the delay between animation steps.
        \return The number of milliseconds between animation steps. By default, the animation delay is set to 40 milliseconds.
        \sa setAnimationDelay
     */
    int animationDelay() const { return m_delay; }

    /*! Returns a Boolean value indicating whether the component is currently animated.
        \return Animation state.
        \sa startAnimation stopAnimation
     */
    bool isAnimated () const;

    /*! Returns a Boolean value indicating whether the receiver shows itself even when it is not animating.
        \return Return true if the progress indicator shows itself even when it is not animating. By default, it returns false.
        \sa setDisplayedWhenStopped
     */
    bool isDisplayedWhenStopped() const;

    /*! Returns the color of the component.
        \sa setColor
      */
    const QColor & color() const { return m_color; }

    virtual QSize sizeHint() const;
    int heightForWidth(int w) const;
public slots:
    /*! Starts the spin animation.
        \sa stopAnimation isAnimated
     */
    void startAnimation();

    /*! Stops the spin animation.
        \sa startAnimation isAnimated
     */
    void stopAnimation();

    /*! Sets the delay between animation steps.
        Setting the \a delay to a value larger than 40 slows the animation, while setting the \a delay to a smaller value speeds it up.
        \param delay The delay, in milliseconds.
        \sa animationDelay
     */
    void setAnimationDelay(int delay);

    /*! Sets whether the component hides itself when it is not animating.
       \param state The animation state. Set false to hide the progress indicator when it is not animating; otherwise true.
       \sa isDisplayedWhenStopped
     */
    void setDisplayedWhenStopped(bool state);

    /*! Sets the color of the components to the given color.
        \sa color
     */
    void setColor(const QColor & color);
protected:
    virtual void timerEvent(QTimerEvent * event);
    virtual void paintEvent(QPaintEvent * event);
private:
    int m_angle;
    int m_timerId;
    int m_delay;
    bool m_displayedWhenStopped;
    QColor m_color;
};

#endif // QPROGRESSINDICATOR_H

QProgressIndicator.cpp

#include "QProgressIndicator.h"

#include <QPainter>

QProgressIndicator::QProgressIndicator(QWidget* parent)
    : QWidget(parent),
      m_angle(0),
      m_timerId(-1),
      m_delay(40),
      m_displayedWhenStopped(false),
      m_color(Qt::black)
{
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    setFocusPolicy(Qt::NoFocus);
}

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

void QProgressIndicator::setDisplayedWhenStopped(bool state)
{
    m_displayedWhenStopped = state;

    update();
}

bool QProgressIndicator::isDisplayedWhenStopped() const
{
    return m_displayedWhenStopped;
}

void QProgressIndicator::startAnimation()
{
    m_angle = 0;

    if (m_timerId == -1)
        m_timerId = startTimer(m_delay);
}

void QProgressIndicator::stopAnimation()
{
    if (m_timerId != -1)
        killTimer(m_timerId);

    m_timerId = -1;

    update();
}

void QProgressIndicator::setAnimationDelay(int delay)
{
    if (m_timerId != -1)
        killTimer(m_timerId);

    m_delay = delay;

    if (m_timerId != -1)
        m_timerId = startTimer(m_delay);
}

void QProgressIndicator::setColor(const QColor & color)
{
    m_color = color;

    update();
}

QSize QProgressIndicator::sizeHint() const
{
    return QSize(20,20);
}

int QProgressIndicator::heightForWidth(int w) const
{
    return w;
}

void QProgressIndicator::timerEvent(QTimerEvent * /*event*/)
{
    m_angle = (m_angle+30)%360;

    update();
}

void QProgressIndicator::paintEvent(QPaintEvent * /*event*/)
{
    if (!m_displayedWhenStopped && !isAnimated())
        return;

    int width = qMin(this->width(), this->height());

    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);

    int outerRadius = (width-1)*0.5;
    int innerRadius = (width-1)*0.5*0.38;

    int capsuleHeight = outerRadius - innerRadius;
    int capsuleWidth  = (width > 32 ) ? capsuleHeight *.23 : capsuleHeight *.35;
    int capsuleRadius = capsuleWidth/2;

    for (int i=0; i<12; i++)
    {
        QColor color = m_color;
        color.setAlphaF(1.0f - (i/12.0f));
        p.setPen(Qt::NoPen);
        p.setBrush(color);
        p.save();
        p.translate(rect().center());
        p.rotate(m_angle - i*30.0f);
        p.drawRoundedRect(-capsuleWidth*0.5, -(innerRadius+capsuleHeight), capsuleWidth, capsuleHeight, capsuleRadius, capsuleRadius);
        p.restore();
    }
}

使用

QProgressIndicator *pIndicator = new QProgressIndicator(this);
pIndicator->setColor(Qt::white);
pIndicator->startAnimation();

源码没什么难度,有兴趣的可以根据需要自行修改。

时间: 2024-09-02 22:11:37

Qt之等待提示框(QProgressIndicator)的相关文章

Qt之等待提示框(QPropertyAnimation)

简述 之前分享过QLabel可以通过QMovie播放gif图片,可以实现等待提示框,今天主要使用动画QPropertyAnimation来进行实现! 数据加载的时候,往往都需要后台线程进行数据请求,而前台需要进行友好性的交互,防止无休止的等待,或者进程卡死. 简述 效果 资源 源码 分析 衍伸 效果 由于录制程序的原因,引起gif效果不清晰,可忽略. 资源 需要几张不同阶段的图标进行切换,这里使用8张. 源码 QPropertyAnimation动画里面并不支持旋转的属性,所以我们可以通过自定义

Qt之等待提示框(QTimer)

简述 上节讲述了关于QPropertyAnimation实现等待提示框的显示,本节我们使用另外一种方案来实现-使用定时器QTimer,通过设置超时时间定时更新图标达到旋转效果. 简述 效果 资源 源码 效果 由于录制程序的原因,引起gif效果不清晰,可忽略. 资源 需要几张不同阶段的图标进行切换,这里使用8张. 源码 QTimer通过setInterval设置100毫秒超时时间,每隔100毫秒后进行图标的更换,达到旋转效果. m_pTimer = new QTimer(this);. // 设定

Qt之等待提示框(QMovie)

简述 关于gif的使用在实际项目中我用的并不多,因为我感觉瑕疵挺多的,很多时候锯齿比较严重,当然与图存在很大的关系. 关于生成gif的方法可以提供一个网站preloaders,基本是可以满足需求的. 简述 效果 源码 效果 由于录制程序的原因,引起gif效果不清晰,可忽略. 源码 通过使用QMovie来设置动画.可以调用setSpeed()设置动画速度.start()启动动画.stop()停止动画等. QMovie *pMovie = new QMovie(":/Images/loading&q

Qt之QProgressIndicator(等待提示框)

简述 很早以前在网上看到一个纯代码实现的旋转动画感觉效果很不错,分享给大家.不得不说,条条大道通罗马,我们需要更多地创造... 详见:QProgressIndicator 简述 效果 源码 使用 更多参考 效果 由于录制程序的原因,引起gif效果不清晰,可忽略. 源码 QProgressIndicator.h #ifndef QPROGRESSINDICATOR_H #define QPROGRESSINDICATOR_H #include <QWidget> #include <QCo

JAVA实现线程等待提示框

Java语言从其诞生到现在不过短短五年时间,却已经成为全球最热门的语言,Java程序员正成为IT业其它程序员中薪金最高的职员.这一切都应归功于Java良好的特性:简单.面向对象.分布式.平台无关性.可移植性.支持多线程等等.本文将用Java的多线程特性来实现线程等待提示框. 1 问题的提出 在Java应用程序编程中,有时需要在GUI(图形化用户界面)中处理一些占用系统资源较多,耗费时间较长的事务,例如:与数据库进行大批量数据交换.大数据量的复杂运算.远程连接服务器等等.系统在处理这些事务时,如果

Qt之透明提示框

简述 经常使用企鹅的小伙伴一定对登录失败的提示框很熟悉,主要涉及窗口透明并添加图标.提示信息.关闭按钮的显示等. 我们可以利用QWidget创建一个提示框,然后通过样式设置我们想要的效果. 简述 效果 源码 样式 效果 源码 QMessageWidget.h #ifndef MESSAGE_WIDGET #define MESSAGE_WIDGET #include <QWidget> class QLabel; class QPushButton; class QMessageWidget

Dev 等待提示 WaitDialogForm 升级版

本文转载:http://www.cnblogs.com/VincentLuo/archive/2011/12/24/2298916.html   一.Dev的等待提示框                                                                                                                                                                      

Windows 8开发入门(五)弹出提示框MessageDialog与await、async关键字

在以前Silverlight.WPF中的弹出窗口提示中是MessageBox类中进行显示的,现在Windows 8中使用 Windows.UI.Popups命名空间下的MessageDialog类代替MessageBox. MessageDialog类有以下常用方法 和属性: ShowAsync():异步弹出消息框. Commands:添加命令,在弹出框界面上同步添加相应的按 钮. DefaultCommandIndex:设置默认按钮的索引,按ENTER键将激活该索引对应的命令按钮 Cancel

Qt之自定义搜索框

简述 关于搜索框,大家都经常接触.例如:浏览器搜索.Windows资源管理器搜索等. 当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定. 简述 效果 细节分析 Coding 源码下载 效果 细节分析 实现细节需要如下步骤: 组合实现,输入框+按钮 事件关联 获取输入文本,进行文本搜索 为了更人性.易用,这里有一些细节需要注意: 输入框的文本不能处于按钮之下 输入框无文本时必须给与友好性提示 按钮无文本描述,一般需要给予ToolTip提示 按钮样式-正常.滑过.按下,以及鼠标滑过鼠标样