Design of the waiting dialog box in Qt and the waiting dialog box in Qt

Source: Internet
Author: User

Design of the waiting dialog box in Qt and the waiting dialog box in Qt

A waiting dialog box is used to prompt the user to operate the Qt program for a long time.
Qt has a standard QProcessDialog to partially implement this function, but the disadvantage is to calculate the progress. Second, you can cancel it halfway. Therefore, we generally use a custom dialog box to implement this function.

Common Implementation Mechanism

Open-source project QtWaitingSpinner. You can download the code and try it. It uses the code to depict the progress bar dynamically. Therefore, you can adjust the ring wait sign of different sizes.
Https://github.com/snowwlex/QtWaitingSpinner

But it is not an independent dialog box, so we use QMovie to play gif to implement the corresponding function.

QWaitDialog dialog box

The idea and material comes from the http://blog.csdn.net/liang19890820/article/details/9067679, but this is only a demonstration of the idea, I implement the complete code here, added the rounded rectangle background

Use the following animated gif

There is also a gif animation production and material library http://preloaders.net/

The qgif library is used for playback and the complete code is provided.
QWaitDialog. h

#ifndef QWAITDIALOG_H#define QWAITDIALOG_H#include <QDialog>#include <QLabel>#include <QMovie>#include <QFrame>class QWaitDialog : public QDialog{    Q_OBJECTpublic:    explicit QWaitDialog(QWidget *parent,const QString &title);    ~QWaitDialog();private:    QMovie *movie;    QLabel *label;    QLabel * tip_label;    QFrame * background;signals:public slots:};#endif // QWAITDIALOG_H

QWaitDialog. cpp

# Include "qwaitdialog. h "QWaitDialog: QWaitDialog (QWidget * parent, const QString & title): QDialog (parent) {this-> setFixedSize (100,100 ); // set the transparency this-> setWindowOpacity (0.8); // cancel the Dialog box title this-> setWindowFlags (Qt: FramelessWindowHint | Qt: Dialog ); this-> setStyleSheet ("background-color: transparent;"); // you can specify background = new QFrame (this) when the background color is light blue ); background-> setStyleSheet ("background-color: rgb (5 5,135,215); border-radius: 10px; "); background-> setGeometry (0, 0,100,100); label = new QLabel (background); label-> setStyleSheet (" background-color: transparent; "); label-> setGeometry (30, 15, 40, 40); movie = new QMovie (":/resource/images/wait3.gif "); label-> setScaledContents (true); label-> setMovie (movie); tip_label = new QLabel (background); tip_label-> setText (title); tip_label-> setStyleSheet ("color: White; background-color: transparent; "); tip_label-> setGeometry (10, 70, 80, 20); movie-> start ();} QWaitDialog ::~ QWaitDialog () {delete label; delete movie; delete tip_label; delete background ;}

The code I added is mainly used to implement the rounded corner window using qss. Here, border-radius is not used in the dialog box because this attribute is invalid for the top-level window. You can only set the background of the dialog box to transparent, and then add a rounded QFrame.

This-> setWindowFlags (Qt: FramelessWindowHint | Qt: Dialog );
This-> setStyleSheet ("background-color: transparent ;");

Background = new QFrame (this );
Background-> setStyleSheet ("background-color: rgb (55,135,215); border-radius: 10px ;");

Final Effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.