ProgressTask Class Reference

Detailed Description

Wrapper around QThread and ProgressDialog that runs a task in the background,
providing updates to the progress bar on the main thread.

Warning: You should always construct one of these with new() as it will outlive the current
scope and delete itself automatically.

Started automatically. Call wait() to wait for completion, or cancel() to cancel.

Example:

Starts task ProgressTask* task = new ProgressTask("Long Operation", "Long Operation", "Cancel", [](std::function<bool(size_t, size_t)> progress) { doLongOperationWithProgress(progress);

Report progress by calling the progress function if (!progress(current, maximum)) return; // If the progress function returns false, then the user has cancelled the operation }); Throws if doLongOperationWithProgress threw task->wait(); Task deletes itself later

Public Slots

void cancel ()
 Cancel the progress dialog. More...
 

Signals

void progress (int cur, int max)
 Signal reported every time there is a progress update (probably often) More...
 
void finished ()
 Signal reported when the task has finished. More...
 

Public Member Functions

 ProgressTask (QWidget *parent, const QString &name, const QString &text, const QString &cancel, std::function< void(std::function< bool(size_t, size_t)>)> func)
 Construct a new progress task, which automatically starts running a given function. More...
 
virtual ~ProgressTask ()
 
bool wait ()
 Wait for the task to finish. More...
 
void hideForModal (std::function< void()> modal)
 Hide the task to present a modal (in a function) since the progress dialog will block other parts of the ui from responding while it is present. More...
 
QString text () const
 Get the text label of the progress dialog. More...
 
void setText (const QString &text)
 Set the text label on the progress dialog. More...
 

Constructor & Destructor Documentation

◆ ProgressTask()

ProgressTask::ProgressTask ( QWidget *  parent,
const QString &  name,
const QString &  text,
const QString &  cancel,
std::function< void(std::function< bool(size_t, size_t)>)>  func 
)

Construct a new progress task, which automatically starts running a given function.

Parameters
parentParent QWidget to display progress dialog on top of
nameTitle for progress dialog
textText for progress dialog
cancelCancel button title. If empty, the cancel button will not be shown
funcFunction to run in the background, which takes a progress reporting function for its argument. The function should call the progress function periodically to signal updates and check for cancellation.

◆ ~ProgressTask()

virtual ProgressTask::~ProgressTask ( )
virtual

Member Function Documentation

◆ cancel

void ProgressTask::cancel ( )
slot

Cancel the progress dialog.

◆ finished

void ProgressTask::finished ( )
signal

Signal reported when the task has finished.

◆ hideForModal()

void ProgressTask::hideForModal ( std::function< void()>  modal)

Hide the task to present a modal (in a function) since the progress dialog will block other parts of the ui from responding while it is present.

Parameters
modalFunction to present a modal ui on top

◆ progress

void ProgressTask::progress ( int  cur,
int  max 
)
signal

Signal reported every time there is a progress update (probably often)

Parameters
curCurrent progress value
maxMaximum progress value

◆ setText()

void ProgressTask::setText ( const QString &  text)

Set the text label on the progress dialog.

Parameters
textNew text label contents

◆ text()

QString ProgressTask::text ( ) const

Get the text label of the progress dialog.

Returns
Text label contents

◆ wait()

bool ProgressTask::wait ( )

Wait for the task to finish.

Exceptions
exceptionAny exception that the provided func throws
Returns
False if canceled, true otherwise

The documentation for this class was generated from the following file: