首页 > 其他分享 >Qt 之 QProgressIndicator(等待提示框)

Qt 之 QProgressIndicator(等待提示框)

时间:2024-05-22 14:56:11浏览次数:23  
标签:const Qt int void delay color 提示框 QProgressIndicator

效果

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

源码

QProgressIndicator.h

 1 #ifndef QPROGRESSINDICATOR_H
 2 #define QPROGRESSINDICATOR_H
 3 
 4 #include <QWidget>
 5 #include <QColor>
 6 
 7 /*! 
 8     \class QProgressIndicator
 9     \brief The QProgressIndicator class lets an application display a progress indicator to show that a lengthy task is under way. 
10 
11     Progress indicators are indeterminate and do nothing more than spin to show that the application is busy.
12     \sa QProgressBar
13 */
14 class QProgressIndicator : public QWidget
15 {
16     Q_OBJECT
17     Q_PROPERTY(int delay READ animationDelay WRITE setAnimationDelay)
18     Q_PROPERTY(bool displayedWhenStopped READ isDisplayedWhenStopped WRITE setDisplayedWhenStopped)
19     Q_PROPERTY(QColor color READ color WRITE setColor)
20 public:
21     QProgressIndicator(QWidget* parent = 0);
22 
23     /*! Returns the delay between animation steps.
24         \return The number of milliseconds between animation steps. By default, the animation delay is set to 40 milliseconds.
25         \sa setAnimationDelay
26      */
27     int animationDelay() const { return m_delay; }
28 
29     /*! Returns a Boolean value indicating whether the component is currently animated.
30         \return Animation state.
31         \sa startAnimation stopAnimation
32      */
33     bool isAnimated () const;
34 
35     /*! Returns a Boolean value indicating whether the receiver shows itself even when it is not animating.
36         \return Return true if the progress indicator shows itself even when it is not animating. By default, it returns false.
37         \sa setDisplayedWhenStopped
38      */
39     bool isDisplayedWhenStopped() const;
40 
41     /*! Returns the color of the component.
42         \sa setColor
43       */
44     const QColor & color() const { return m_color; }
45 
46     virtual QSize sizeHint() const;
47     int heightForWidth(int w) const;
48 public slots:
49     /*! Starts the spin animation.
50         \sa stopAnimation isAnimated
51      */
52     void startAnimation();
53 
54     /*! Stops the spin animation.
55         \sa startAnimation isAnimated
56      */
57     void stopAnimation();
58 
59     /*! Sets the delay between animation steps.
60         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.
61         \param delay The delay, in milliseconds. 
62         \sa animationDelay 
63      */
64     void setAnimationDelay(int delay);
65 
66     /*! Sets whether the component hides itself when it is not animating. 
67        \param state The animation state. Set false to hide the progress indicator when it is not animating; otherwise true.
68        \sa isDisplayedWhenStopped
69      */
70     void setDisplayedWhenStopped(bool state);
71 
72     /*! Sets the color of the components to the given color.
73         \sa color
74      */
75     void setColor(const QColor & color);
76 protected:
77     virtual void timerEvent(QTimerEvent * event); 
78     virtual void paintEvent(QPaintEvent * event);
79 private:
80     int m_angle;
81     int m_timerId;
82     int m_delay;
83     bool m_displayedWhenStopped;
84     QColor m_color;
85 };
86 
87 #endif // QPROGRESSINDICATOR_H

QProgressIndicator.cpp

  1 #include "QProgressIndicator.h"
  2 
  3 #include <QPainter>
  4 
  5 QProgressIndicator::QProgressIndicator(QWidget* parent)
  6     : QWidget(parent),
  7       m_angle(0),
  8       m_timerId(-1),
  9       m_delay(40),
 10       m_displayedWhenStopped(false),
 11       m_color(Qt::black)
 12 {
 13     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 14     setFocusPolicy(Qt::NoFocus);
 15 }
 16 
 17 bool QProgressIndicator::isAnimated () const
 18 {
 19     return (m_timerId != -1);
 20 }
 21 
 22 void QProgressIndicator::setDisplayedWhenStopped(bool state)
 23 {
 24     m_displayedWhenStopped = state;
 25 
 26     update();
 27 }
 28 
 29 bool QProgressIndicator::isDisplayedWhenStopped() const
 30 {
 31     return m_displayedWhenStopped;
 32 }
 33 
 34 void QProgressIndicator::startAnimation()
 35 {
 36     m_angle = 0;
 37 
 38     if (m_timerId == -1)
 39         m_timerId = startTimer(m_delay);
 40 }
 41 
 42 void QProgressIndicator::stopAnimation()
 43 {
 44     if (m_timerId != -1)
 45         killTimer(m_timerId);
 46 
 47     m_timerId = -1;
 48 
 49     update();
 50 }
 51 
 52 void QProgressIndicator::setAnimationDelay(int delay)
 53 {
 54     if (m_timerId != -1)
 55         killTimer(m_timerId);
 56 
 57     m_delay = delay;
 58 
 59     if (m_timerId != -1)
 60         m_timerId = startTimer(m_delay);
 61 }
 62 
 63 void QProgressIndicator::setColor(const QColor & color)
 64 {
 65     m_color = color;
 66 
 67     update();
 68 }
 69 
 70 QSize QProgressIndicator::sizeHint() const
 71 {
 72     return QSize(20,20);
 73 }
 74 
 75 int QProgressIndicator::heightForWidth(int w) const
 76 {
 77     return w;
 78 }
 79 
 80 void QProgressIndicator::timerEvent(QTimerEvent * /*event*/)
 81 {
 82     m_angle = (m_angle+30)%360;
 83 
 84     update();
 85 }
 86 
 87 void QProgressIndicator::paintEvent(QPaintEvent * /*event*/)
 88 {
 89     if (!m_displayedWhenStopped && !isAnimated())
 90         return;
 91 
 92     int width = qMin(this->width(), this->height());
 93     
 94     QPainter p(this);
 95     p.setRenderHint(QPainter::Antialiasing);
 96     
 97     int outerRadius = (width-1)*0.5;
 98     int innerRadius = (width-1)*0.5*0.38;
 99 
100     int capsuleHeight = outerRadius - innerRadius;
101     int capsuleWidth  = (width > 32 ) ? capsuleHeight *.23 : capsuleHeight *.35;
102     int capsuleRadius = capsuleWidth/2;
103 
104     for (int i=0; i<12; i++)
105     {
106         QColor color = m_color;
107         color.setAlphaF(1.0f - (i/12.0f));
108         p.setPen(Qt::NoPen);
109         p.setBrush(color);       
110         p.save();
111         p.translate(rect().center());
112         p.rotate(m_angle - i*30.0f);
113         p.drawRoundedRect(-capsuleWidth*0.5, -(innerRadius+capsuleHeight), capsuleWidth, capsuleHeight, capsuleRadius, capsuleRadius);
114         p.restore();
115     }
116 }

使用

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

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

标签:const,Qt,int,void,delay,color,提示框,QProgressIndicator
From: https://www.cnblogs.com/ybqjymy/p/18206252

相关文章

  • LLM-文心一言:modbus、opc、can、mqtt协议
    Modbus、OPC、CAN和MQTT都是不同的通信协议,它们在工业自动化、物联网和其他领域有着广泛的应用。以下是对这些协议的简要介绍:Modbus:Modbus是一种串行通信协议,由Modicon公司(现为施耐德电气的一部分)在1979年提出,用于可编程逻辑控制器(PLC)之间的通信。它已经成为工业领域通信协议的......
  • Ubuntu上使用QT creator运行cuda程序 转载的文章
    突发奇想想尝试一下QT界面中使用CUDA加速过的程序,然后查了一下资料,总结一下有以下几点吧1、CUDA配置全部放在.pro文件中2、main.cpp为主函数使用g++编译3、kernel.cu为核函数使用nvcc编译不多说上代码以下为main.cpp代码   #include<QtCore/QCoreApplication>       ......
  • Qt QMovie无法显示gif动画的一种解决方法
    注:本来用这种方法显示gif动画是没有问题的。问题:如下方法,槽函数startGif()如下,点击按钮无法出现gif动画分析:gif动画资源确实是加载进来了,但是在执行movie.start()时,整个资源就已经消失了,所以无法显示动画。要使资源长期存在需要用到c中的修饰符: static。1voidshowGif::st......
  • Qt QMovie播放gif(播放、暂停和继续)
    有些时候我们需要展示一个gif图片,可是用了普通的方法发现不行。啊哦,这就是生活中的理解和程序猿的区别了,程序猿眼中的gif不是图片,而是一个movie。QT提供了QMovie来进行操作。演示示例是通过两个按钮来进行操控,一个负责暂停一个负责进行继续播放。首先要在头文件中声明这个类,一......
  • qt如何将下拉框的框设置为圆角矩形
    在Qt中,可以使用样式表(QSS)来设置下拉框的外观,包括圆角矩形的样式。以下是一个例子://在C++代码中设置样式表QComboBox{border-radius:8px;/*设置圆角半径*/background-color:#FFFFFF;/*设置背景颜色*/color:#333333;/*设置文字颜色*/p......
  • Qt 动画播放之QMovie类
    主要是用到QMovie类实现在事件触发时开启动画播放效果(需要注意的是,这个动画播放默认是循环播放的,如果不做特殊处理动画会一直播放)1QMovie*movie=newQMovie("aaa.gif");2ui->movieLabel->setMovie(movie);3movie->start();//启动gif图片4//movie->......
  • qt之点的绘制示例demo
    #include"mainwindow.h"#include"ui_mainwindow.h"#include<QPainter>#include<QColor>QColorm_color;intm_x=0;intm_y=0;intm_w=0;intm_h=0;MainWindow::MainWindow(QWidget*parent):QMainWindow(parent)......
  • Qt 程序启动画面QSplashScreen
    QSplashScreen是Qt框架提供的一个类,用于在应用程序启动时显示一个带有文本和图像的启动画面(SplashScreen)。使用QSplashScreen类,你可以在应用程序启动时显示一个自定义的启动画面,以提供更好的用户体验。这个类提供了一些方法和属性,可以让你设置启动画面的文本、图像和其......
  • qt中添加多个UI界面,并在其他文件中调用该UI界面
    一、概述qt创建一个新项目时,会创建一个属于mainwindow'的主ui界面,这时如果想要创建多个窗口,可以有三种方法。第一种:使用代码创建一个新窗口,优点是灵活,缺点是麻烦,其中控件和布局都需要代码来创建。第二种:在ui设计界面使用tabwidget控件来达到多个窗口的效果,不过这种方法只能......
  • qt拖动窗口
    voida::mousePressEvent(QMouseEvent*event){if(event->button()==Qt::LeftButton&&event->pos().y()<(this->height()-mainWidget->height())){m_dragging=true;m_dragStarPos=event->pos();}}voida::......