Qt下等待對話方塊的設計,Qt等待對話方塊設計

來源:互聯網
上載者:User

Qt下等待對話方塊的設計,Qt等待對話方塊設計

在Qt程式經常長時間操作時採用一個等待對話方塊來提示使用者正在操作。
Qt有標準的QProcessDialog 能部分實現這個功能,但是缺點是要計算進度,二是使用者可以中途取消。所以一般我們採用自訂對話方塊實現這個功能。

常見實現機制

開源項目 QtWaitingSpinner,具體可以下載代碼一試,它是自己用代碼描繪出進度條動態來了,因此可以調整出不同大小的環形等待標誌
https://github.com/snowwlex/QtWaitingSpinner

但是他不是一個獨立對話方塊,因此我們更多使用QMovie播放gif來實現對應功能

QWaitDialog對話方塊

其中的思路和素材來源於http://blog.csdn.net/liang19890820/article/details/9067679, 但這個只一個思路示範,我這裡實現是完整代碼,增加了圓角矩形背景

使用如下動畫gif

這裡還有一個gif 動畫的製作和素材庫 http://preloaders.net/

其中播放使用qgif庫,完整 代碼
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);      //設定透明度     this->setWindowOpacity(0.8);      //取消對話方塊標題      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;");      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;}

其中,是我加的代碼,主要實現用qss實現圓角視窗。這裡不在對話方塊採用border-radius是因為這個屬性對頂層視窗無效,只能先設對話方塊背景為透明,再上面鋪一個圓角的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;”);

最終效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.