Qt QFutureWatcher


簡述

QFuture 表示異步計算的結果,QFutureWatcher 則允許使用信號和槽監視 QFuture,也就是說,QFutureWatcher 是為 QFuture 而生的。

詳細描述

QFutureWatcher 提供了有關 QFuture 的信息和通知,使用 setFuture() 函數開始監視一個特定的 QFuture,函數 future() 則返回由 setFuture() 設置的 future。

為了方便,QFuture 的很多函數可以直接通過 QFutureWatcher 來訪問,例如:progressValue()、progressMinimum()、progressMaximum()、progressText()、isStarted()、isFinished()、isRunning()、isCanceled()、isPaused()、waitForFinished()、result() 和 resultAt()。而 cancel()、setPaused()、pause()、resume() 和 togglePaused() 是 QFutureWatcher 中的槽函數。

狀態更改由 started()、finished()、cancelled()、paused()、resumed()、resultReadyAt() 和 resultsReadyAt() 信號提供,進度信息由 progressRangeChanged()、progressValueChanged() 和progressTextChanged() 信號提供。

由函數 setPendingResultsLimit() 提供節流控制。當掛起的 resultReadyAt() 或 resultsReadyAt() 信號數量超過限制時,由 future 表示的計算將被自動節流。一旦掛起的信號數量下降到限制以下時,計算將恢復。

示例,開始計算並當完成時獲取槽回調:

1 // 實例化對象,並連接到 finished() 信號。
2 MyClass myObject; 3 QFutureWatcher<int> watcher; 4 connect(&watcher, SIGNAL(finished()), &myObject, SLOT(handleFinished())); 5  
6 // 開始計算
7 QFuture<int> future = QtConcurrent::run(...); 8 watcher.setFuture(future);

基本使用

來看一個圖像加載和縮放的示例。選擇多個圖片,進行異步計算(將所有圖片進行縮放),加載過程中可以顯示進度,以便我們實時了解進展。每當一個圖片處理完成,就會顯示在窗體中。

這里僅為了演示效果,加載了 8 張 圖片。

這里寫圖片描述

具體的源碼如下所示:

ImagesView.h:

 1 #ifndef IMAGES_VIEW_H  2 #define IMAGES_VIEW_H
 3  
 4 #include <QFutureWatcher>
 5 #include <QWidget>
 6  
 7 class QLabel;  8 class QPushButton;  9 class QVBoxLayout; 10 class QGridLayout; 11  
12 class ImagesView : public QWidget 13 { 14  Q_OBJECT 15  
16 public: 17     explicit ImagesView(QWidget *parent = 0); 18     ~ImagesView(); 19  
20 private slots: 21     void open();  // 打開目錄,加載圖片
22     void showImage(int index);  // 顯示圖片
23     void finished();  // 更新按鈕狀態
24  
25 private: 26     QPushButton *m_pOpenButton; 27     QPushButton *m_pCancelButton; 28     QPushButton *m_pPauseButton; 29     QVBoxLayout *m_pMainLayout; 30     QGridLayout *m_pImagesLayout; 31     QList<QLabel *> labels; 32     QFutureWatcher<QImage> *m_pWatcher; 33 }; 34  
35 #endif // IMAGES_VIEW_H

下面是實現部分,c_nImageSize 表示的是圖片被縮放的大小(寬度:100 px,高度:100px),函數 scale() 則是對圖片縮放的具體實現。

ImagesView.cpp

 1 #include <QLabel>
 2 #include <QPushButton>
 3 #include <QProgressBar>
 4 #include <QFileDialog>
 5 #include <QtConcurrent/QtConcurrentMap>
 6 #include <QStandardPaths>
 7 #include <QHBoxLayout>
 8 #include <qmath.h>
 9 #include "ImagesView.h"
 10  
 11 const int c_nImageSize = 100;  12  
 13 // 縮放圖片
 14 QImage scale(const QString &imageFileName)  15 {  16  QImage image(imageFileName);  17     return image.scaled(QSize(c_nImageSize, c_nImageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  18 }  19  
 20 ImagesView::ImagesView(QWidget *parent)  21  : QWidget(parent)  22 {  23     setWindowIcon(QIcon(":/Images/logo"));  24     setWindowTitle(QStringLiteral("Qt之QFutureWatcher"));  25     resize(800, 600);  26  
 27     // 初始化控件
 28     m_pWatcher = new QFutureWatcher<QImage>(this);  29     m_pOpenButton = new QPushButton(QStringLiteral("打開圖片"));  30     m_pCancelButton = new QPushButton(QStringLiteral("取消"));  31     m_pPauseButton = new QPushButton(QStringLiteral("暫停/恢復"));  32     QProgressBar *pProgressBar = new QProgressBar(this);  33  
 34     m_pCancelButton->setEnabled(false);  35     m_pPauseButton->setEnabled(false);  36  
 37     // 布局
 38     QHBoxLayout *pButtonLayout = new QHBoxLayout();  39     pButtonLayout->addWidget(m_pOpenButton);  40     pButtonLayout->addWidget(m_pCancelButton);  41     pButtonLayout->addWidget(m_pPauseButton);  42     pButtonLayout->addStretch();  43     pButtonLayout->setSpacing(10);  44     pButtonLayout->setMargin(0);  45  
 46     m_pImagesLayout = new QGridLayout();  47  
 48     m_pMainLayout = new QVBoxLayout();  49     m_pMainLayout->addLayout(pButtonLayout);  50     m_pMainLayout->addWidget(pProgressBar);  51     m_pMainLayout->addLayout(m_pImagesLayout);  52     m_pMainLayout->addStretch();  53     m_pMainLayout->setSpacing(10);  54     m_pMainLayout->setContentsMargins(10, 10, 10, 10);  55  setLayout(m_pMainLayout);  56  
 57     // 連接信號槽 - 加載、顯示進度、打開、取消等操作
 58     connect(m_pWatcher, SIGNAL(resultReadyAt(int)), SLOT(showImage(int)));  59     connect(m_pWatcher, SIGNAL(progressRangeChanged(int,int)), pProgressBar, SLOT(setRange(int,int)));  60     connect(m_pWatcher, SIGNAL(progressValueChanged(int)), pProgressBar, SLOT(setValue(int)));  61  connect(m_pWatcher, SIGNAL(finished()), SLOT(finished()));  62  connect(m_pOpenButton, SIGNAL(clicked()), SLOT(open()));  63  connect(m_pCancelButton, SIGNAL(clicked()), m_pWatcher, SLOT(cancel()));  64  connect(m_pPauseButton, SIGNAL(clicked()), m_pWatcher, SLOT(togglePaused()));  65 }  66  
 67 ImagesView::~ImagesView()  68 {  69     m_pWatcher->cancel();  70     m_pWatcher->waitForFinished();  71 }  72  
 73 // 打開目錄,加載圖片
 74 void ImagesView::open()  75 {  76     // 如果已經加載圖片,取消並進行等待
 77     if (m_pWatcher->isRunning()) {  78         m_pWatcher->cancel();  79         m_pWatcher->waitForFinished();  80  }  81  
 82     // 顯示一個文件打開對話框
 83     QStringList files = QFileDialog::getOpenFileNames(this,  84                                                       QStringLiteral("選擇圖片"),  85  QStandardPaths::writableLocation(QStandardPaths::PicturesLocation),  86                                                       "*.jpg *.png");  87  
 88     if (files.count() == 0)  89         return;  90  
 91     // 做一個簡單的布局
 92  qDeleteAll(labels);  93  labels.clear();  94  
 95     int dim = qSqrt(qreal(files.count())) + 1;  96     for (int i = 0; i < dim; ++i) {  97         for (int j = 0; j < dim; ++j) {  98             QLabel *pLabel = new QLabel(this);  99             pLabel->setFixedSize(c_nImageSize, c_nImageSize); 100             m_pImagesLayout->addWidget(pLabel, i, j); 101  labels.append(pLabel); 102  } 103  } 104  
105     // 使用 mapped 來為 files 運行線程安全的 scale 函數
106     m_pWatcher->setFuture(QtConcurrent::mapped(files, scale)); 107  
108     m_pOpenButton->setEnabled(false); 109     m_pCancelButton->setEnabled(true); 110     m_pPauseButton->setEnabled(true); 111 } 112  
113 // 顯示圖片
114 void ImagesView::showImage(int index) 115 { 116     labels[index]->setPixmap(QPixmap::fromImage(m_pWatcher->resultAt(index))); 117 } 118  
119 // 更新按鈕狀態
120 void ImagesView::finished() 121 { 122     m_pOpenButton->setEnabled(true); 123     m_pCancelButton->setEnabled(false); 124     m_pPauseButton->setEnabled(false); 125 }

構造函數中,需要注意的是槽函數,其中 resultReadyAt() 表示 index 對應位置的處理結果已准備就緒,所以連接該信號至槽函數 showImage(),可以顯示處理完的圖片。

為了顯示處理進度,我們構造了一個進度條,當 QFutureWatcher 的 progressRangeChanged() 的信號發射時,進度條的范圍會發生改變,而 progressValueChanged() 信號發射時,會更新進度條的值。

如果加載的圖片較多時,可以通過點擊“取消”按鈕,這時會調用 QFutureWatcher 的 cancel() 槽函數來取消計算。“暫停/恢復”則調用 togglePaused() 槽函數,用於切換異步計算的暫停狀態,換句話說,如果計算當前已暫停,調用此函數將進行恢復;如果計算正在運行,則會暫停。

當點擊“打開”按鈕時,會調用槽函數 open(),默認打開圖片目錄,以便進行圖片的選擇。然后根據圖片創建對應數量的標簽 QLabel,用於顯示后期縮放的圖片。創建完成后,使用 mapped() 進行並行計算,並添加至 QFutureWatcher 中,讓其使用信號和槽監視 QFuture。

接下來,就可以直接使用了。

 1 #include <QApplication>
 2 #include "ImagesView.h"
 3  
 4 int main(int argc, char *argv[])  5 {  6  QApplication app(argc,argv);  7  
 8  ImagesView view;  9  view.show(); 10  
11     return app.exec(); 12 }

這樣,我們就完成了一個圖片縮放加載縮放的功能。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM