(Qt6 - Advanced) 5. QFutureWatcher

Posted by : at

Category : Qt


#include <QCoreApplication>
#include <QDebug>
#include <QFuture>
#include <QFutureWatcher>
#include <QList>
#include <QtConcurrent>
#include "test.h"

void multiply(int &value)
{
    qInfo() << "Called: " << value;
    value = value * 10;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QList<int> list;
    for(int i = 0; i < 100; i++)
    {
        list.append(i);
    }

    QFutureWatcher<void> watcher;
    Test test;
    QObject::connect(&watcher, &QFutureWatcher<void>::finished,&test,&Test::finished); //Auto connect "should" work

    QFuture<void> future = QtConcurrent::map(list,&multiply);
    watcher.setFuture(future);

    qInfo() << "Back in main!";

     watcher.waitForFinished(); //Blocking

     qInfo() << "Done!";
     qInfo() << list;

    return a.exec();
}

QFutureWatcher를 Slot으로 받기

#include "test.h"

Test::Test(QObject *parent) : QObject(parent)
{
    //Connect
       /*
       void canceled();
       void finished();
       void paused();
       void progressRangeChanged(int minimum, int maximum);
       void progressTextChanged(const QString &progressText);
       void progressValueChanged(int progressValue);
       void resultReadyAt(int index);
       void resultsReadyAt(int beginIndex, int endIndex);
       void resumed();
       void started();
       */

       connect(&watcher, &QFutureWatcher<void>::canceled, this, &Test::canceled);
       connect(&watcher, &QFutureWatcher<void>::finished, this, &Test::finished);

       //Qt5
       //connect(&watcher, &QFutureWatcher<void>::paused, this, &Test::paused);

       connect(&watcher, &QFutureWatcher<void>::suspending, this, &Test::suspending);
       connect(&watcher, &QFutureWatcher<void>::suspended, this, &Test::suspended);

       connect(&watcher, &QFutureWatcher<void>::progressRangeChanged, this, &Test::progressRangeChanged);
       connect(&watcher, &QFutureWatcher<void>::progressTextChanged, this, &Test::progressTextChanged);
       connect(&watcher, &QFutureWatcher<void>::progressValueChanged, this, &Test::progressValueChanged);
       connect(&watcher, &QFutureWatcher<void>::resultReadyAt, this, &Test::resultReadyAt);
       connect(&watcher, &QFutureWatcher<void>::resumed, this, &Test::resumed);
       connect(&watcher, &QFutureWatcher<void>::started, this, &Test::started);

}

void Test::process()
{
    QMutex mutex;
    mutex.lock();
    list.clear();
    for(int i = 0; i < 100; i++)
    {
        list.append(i);
    }
    mutex.unlock();

    future = QtConcurrent::map(list,&Test::multiply);
    watcher.setFuture(future);
}

void Test::multiply(int &value)
{
    qInfo() << "Called" << value;
    value = value * 10;

    //Slow this down
    QThread::currentThread()->msleep(250);
}

void Test::canceled()
{
    qInfo() << "Canceled";
}

void Test::finished()
{
    qInfo() << "Finished";
    qInfo() << list;
}

void Test::suspending()
{
    qInfo() << "Suspending";
}

void Test::suspended()
{
    qInfo() << "Suspended";
}

void Test::progressRangeChanged(int minimum, int maximum)
{
    qInfo() << "Range " << minimum << " to " << maximum;
}

void Test::progressTextChanged(const QString &progressText)
{
    qInfo() << "Text: " << progressText;
}

void Test::progressValueChanged(int progressValue)
{
    qInfo() << "Progress: " << progressValue;
}

void Test::resultReadyAt(int index)
{
    qInfo() << "ReadyAt: " << index;
}

void Test::resultsReadyAt(int beginIndex, int endIndex)
{
    qInfo() << "ResultsReadyAt " << beginIndex << " to " << endIndex;
}

void Test::resumed()
{
    qInfo() << "Resumed";
}

void Test::started()
{
    qInfo() << "Started";
}

About Taehyung Kim

안녕하세요? 8년차 현업 C++ 개발자 김태형이라고 합니다. 😁 C/C++을 사랑하며 다양한 사람과의 협업을 즐깁니다. ☕ 꾸준한 자기개발을 미덕이라 생각하며 노력중이며, 제가 얻은 지식을 홈페이지에 정리 중입니다. 좀 더 상세한 제 이력서 혹은 Private 프로젝트 접근 권한을 원하신다면 메일주세요. 😎

Star
Useful Links