Briefly
Previously shared qlabel can play GIF picture through Qmovie, can implement wait prompt box, today mainly use animation qpropertyanimation to implement!
Data loading often requires a background thread to make data requests, while the foreground requires friendly interaction, preventing endless waiting, or the process is stuck.
- Briefly
- Effect
- Resources
- Source
- Analysis
- Yan Extension
Effect
Because of the recording program, the GIF effect is not clear, can be ignored.
Resources
You need several different stages of the icon to switch, here use 8.
Source
Qpropertyanimation animations do not support rotation properties, so we can do this by customizing them.
Class MainWindow: Publiccustomwindow{Q_object//Declaration PropertiesQ_property (intPixmap READ pixmap WRITE setpixmap) Public:Explicit MainWindow(Qwidget *parent =0); ~mainwindow ();//Start animation voidStartanimation ();//Stop animation voidStopAnimation ();Private:int Pixmap()Const;//Toggle icon voidSetpixmap (Const intindex);Private: Qlabel *m_ploadinglabel; Qpropertyanimation *m_panimation;intM_nindex;};
m_panimation = new Qpropertyanimation (this , "pixmap" ); m_ Panimation->setduration (1000 ); //run forever until stop m_panimation->setloopcount (-1 ); //set the value of each interval of the animation for (int i = 0 ; i < 8 ; ++i) {m_panimation->setkeyvalueat (I/7.0 , i + 1 );} //start animation startanimation ();
void MainWindow::startAnimation(){ m_pAnimation->start();}void MainWindow::stopAnimation(){ m_pAnimation->stop();}int MainWindow::pixmap() const{ return m_nIndex;}void MainWindow::setPixmap(const int index){ = index; QPixmap pixmap(QString(":/Images/loading%1").arg(index)); m_pLoadingLabel->setPixmap(pixmap);}
Analysis
First we declare the property through Q_property, so that we can use in the Qpropertyanimation animation, and then by setting the length of the animation, the number of runs, the values of each interval to achieve the effect of the toggle icon, and then the animation can be very smooth implementation. Because there are 8 icons, the range is set by Setkeyvalueat (i/7.0, i + 1).
Yan Extension
Above we use 8 icons to switch, another way-only one card, and then the icon rotation.
//set the value of the animation intervals Span class= "Hljs-keyword" >for (int i = 0 ; I <= 10 ; ++i) {m_panimation->setkeyvalueat (I/10.0 , i);} void Mainwindow::setpixmap (const int index ) {m_ NIndex = index ; //icon rotation Qmatrix Leftmatrix; Leftmatrix.rotate ((360.0 /10 ) * index ); Qpixmap Pixmap (QString ()); QPIXMAP pix = pixmap.transformed (Leftmatrix, qt::smoothtransformation); M_ploadinglabel->setpixmap (PIX);}
Although only a picture here, but there is a problem, because the picture is 28*28, rotation angle if not 90 degrees of integer, then, after the rotation of the picture will certainly be scaled, this can cause the top and bottom bounce, so we need to pin the icon to a fixed-size control inside, Prevent this situation from occurring.
qt waiting Cue box (qpropertyanimation)