Loading...
Searching...
No Matches
ProgressTask

Detailed Description

Classes

class  ProgressDialog
 Dialog displaying a progress bar and cancel button. More...
 
class  ProgressTask
 Wrapper around QThread and ProgressDialog that runs a task in the background, providing updates to the progress bar on the main thread. More...
 

Class Documentation

◆ ProgressDialog

class ProgressDialog

Dialog displaying a progress bar and cancel button.

Public Slots

void update (int cur, int total)
 
void cancel ()
 

Signals

void canceled ()
 

Public Member Functions

 ProgressDialog (QWidget *parent, const QString &title, const QString &text, const QString &cancel=QString())
 
bool wasCancelled () const
 
void hideForModal (std::function< void()> modal)
 
QString text () const
 
void setText (const QString &text)
 

Protected Member Functions

virtual void keyPressEvent (QKeyEvent *event) override
 

Constructor & Destructor Documentation

◆ ProgressDialog()

ProgressDialog::ProgressDialog ( QWidget * parent,
const QString & title,
const QString & text,
const QString & cancel = QString() )

Member Function Documentation

◆ wasCancelled()

bool ProgressDialog::wasCancelled ( ) const

◆ hideForModal()

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

◆ text()

QString ProgressDialog::text ( ) const

◆ setText()

void ProgressDialog::setText ( const QString & text)

◆ keyPressEvent()

virtual void ProgressDialog::keyPressEvent ( QKeyEvent * event)
overrideprotectedvirtual

◆ update

void ProgressDialog::update ( int cur,
int total )
slot

◆ cancel

void ProgressDialog::cancel ( )
slot

◆ canceled

void ProgressDialog::canceled ( )
signal

◆ ProgressTask

class ProgressTask

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
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.
bool wait()
Wait for the task to finish.
void progress(int cur, int max)
Signal reported every time there is a progress update (probably often)
Wrapper around QThread and ProgressDialog that runs a task in the background, providing updates to th...
Definition progresstask.h:106

Public Slots

void cancel ()
 Cancel the progress dialog.
 

Signals

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

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.
 
virtual ~ProgressTask ()
 
bool wait ()
 Wait for the task to finish.
 
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.
 
QString text () const
 Get the text label of the progress dialog.
 
void setText (const QString &text)
 Set the text label on the progress dialog.
 

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

◆ wait()

bool ProgressTask::wait ( )

Wait for the task to finish.

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

◆ 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

◆ text()

QString ProgressTask::text ( ) const

Get the text label of the progress dialog.

Returns
Text label contents

◆ setText()

void ProgressTask::setText ( const QString & text)

Set the text label on the progress dialog.

Parameters
textNew text label contents

◆ cancel

void ProgressTask::cancel ( )
slot

Cancel the progress dialog.

◆ 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

◆ finished

void ProgressTask::finished ( )
signal

Signal reported when the task has finished.