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... | |
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.
parent | Parent QWidget to display progress dialog on top of |
name | Title for progress dialog |
text | Text for progress dialog |
cancel | Cancel button title. If empty, the cancel button will not be shown |
func | Function 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. |
|
virtual |
|
slot |
Cancel the progress dialog.
|
signal |
Signal reported when the task has finished.
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.
modal | Function to present a modal ui on top |
|
signal |
Signal reported every time there is a progress update (probably often)
cur | Current progress value |
max | Maximum progress value |
void ProgressTask::setText | ( | const QString & | text | ) |
Set the text label on the progress dialog.
text | New text label contents |
QString ProgressTask::text | ( | ) | const |
Get the text label of the progress dialog.
bool ProgressTask::wait | ( | ) |
Wait for the task to finish.
exception | Any exception that the provided func throws |