You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library introduces *async values*. Like std::future<T> *async value* holds the *result* of async operation or *error* (if operation was unsuccessfull).
7
+
Also it holds *progress* if async operation is still executing and has *stateChanged* signal that fires when async value is switching between progress, result and error.
8
+
9
+
10
+
Like `future<T> std::async(Callable fn)` qt-async library has a set of functions `bool asyncValueRunXXX(AsyncValue& value, Callable fn, ProgressArgs... args)` to make asyncronious calculation for the *value*.
11
+
All these functions switch *value* to a progress state (and pass *args* to progress constructor) and invoke `fn(progress, value)` so *fn* implementation can use *progress* to report executing progress or ask if execution was requested to stop.
12
+
*value* parameter is used to set a result of the operation or an error if calculation has failed.
13
+
14
+
15
+
To represent such *async values* in GUI qt-async library has *async widgets*. Every time *async value* state changes, *async widget* creates sub-widget for that state (ProgressWidget, ErrorWidget or ValueWidget).
16
+
Similar to *async value* where user should supply 'fn' that actually calculates value, user should supply to *async widget* a factory how to create ValueWidget (it can be overriden virtual function or std::function).
17
+
5
18
# Basic use
6
19
7
-
Declare async value object:
20
+
Declare async value object that holds values of type QString and initialized with string "Hello World!":
@@ -17,8 +30,9 @@ When you need to calculate value, call one of the `asyncValueRunXXX` functions:
17
30
// a long calculations
18
31
for (auto i : {0, 1, 2, 3, 4})
19
32
{
20
-
// report progress
33
+
// report progress i/5
21
34
progress.setProgress(i, 5);
35
+
22
36
// check if calculation was stopped
23
37
if (progress.isStopRequested())
24
38
{
@@ -30,6 +44,7 @@ When you need to calculate value, call one of the `asyncValueRunXXX` functions:
30
44
QThread::sleep(1);
31
45
}
32
46
47
+
// do final processing
33
48
progress.setProgress(1.f);
34
49
progress.setMessage("Processing...");
35
50
QThread::sleep(1);
@@ -44,14 +59,14 @@ The available functions are:
44
59
*[asyncValueRunThread](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunThread.h#L23) - creates QThread, does calculations and deletes QThread (don't use this function)
45
60
*[asyncValueRunThreadPool](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunThreadPool.h#L24) - does calculation in a Qt thread pool
46
61
*[asyncValueRunNetwork](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunNetwork.h#L24) - waits QNetworkReply and does calculation from it.
47
-
See tests for examples.
62
+
See [runInThread](https://github.com/lexxmark/qt-async/blob/40af2b9e0a07f8d5cae1e62e039c36012b4234d0/tests/TestAsyncValue.cpp#L48) and [runInThreadPool](https://github.com/lexxmark/qt-async/blob/40af2b9e0a07f8d5cae1e62e039c36012b4234d0/tests/TestAsyncValue.cpp#L62)tests for examples.
48
63
49
64
Somewhere in GUI code declare async widget:
50
65
```C++
51
66
// create widget
52
67
auto valueWidget = new AsyncWidgetFn<AsyncQString>(parent);
53
68
54
-
// set callback that creates widget to show value
69
+
// set callback that creates sub-widget to show QString value
0 commit comments