Skip to content

Commit 84663af

Browse files
committed
update readme
1 parent 40af2b9 commit 84663af

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
This library has some usefull Qt classes and widgets to show asyncronious operations.
1+
This library has some usefull Qt classes and widgets for asyncronious operations.
22

33
[![qt-async demo video](https://img.youtube.com/vi/aTXOpmVRXq0/maxresdefault.jpg)](https://youtu.be/aTXOpmVRXq0)
44

5+
# Brief overview
6+
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+
518
# Basic use
619

7-
Declare async value object:
20+
Declare async value object that holds values of type QString and initialized with string "Hello World!":
821
```C++
922
using AsyncQString = AsyncValue<QString>;
1023
AsyncQString value(AsyncInitByValue{}, "Hello World!");
@@ -17,8 +30,9 @@ When you need to calculate value, call one of the `asyncValueRunXXX` functions:
1730
// a long calculations
1831
for (auto i : {0, 1, 2, 3, 4})
1932
{
20-
// report progress
33+
// report progress i/5
2134
progress.setProgress(i, 5);
35+
2236
// check if calculation was stopped
2337
if (progress.isStopRequested())
2438
{
@@ -30,6 +44,7 @@ When you need to calculate value, call one of the `asyncValueRunXXX` functions:
3044
QThread::sleep(1);
3145
}
3246
47+
// do final processing
3348
progress.setProgress(1.f);
3449
progress.setMessage("Processing...");
3550
QThread::sleep(1);
@@ -44,14 +59,14 @@ The available functions are:
4459
* [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)
4560
* [asyncValueRunThreadPool](https://github.com/lexxmark/qt-async/blob/master/qt-async-lib/values/AsyncValueRunThreadPool.h#L24) - does calculation in a Qt thread pool
4661
* [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.
4863

4964
Somewhere in GUI code declare async widget:
5065
```C++
5166
// create widget
5267
auto valueWidget = new AsyncWidgetFn<AsyncQString>(parent);
5368

54-
// set callback that creates widget to show value
69+
// set callback that creates sub-widget to show QString value
5570
valueWidget->createValueWidget = [](QString& value, QWidget* parent) {
5671
// create QLabel
5772
return AsyncWidgetProxy::createLabel(value, parent);
@@ -112,22 +127,24 @@ To get the content of the async value use `access` functions and supply callable
112127
bool success = value.accessValue([](int value) { /* access int value here */ });
113128
```
114129
115-
User can assign value using following functions:
130+
User can assign a value using the following functions:
116131
```C++
117132
AsyncValue<std::string> value(...);
118133
119134
// create value by passing value's constructor parameters
120135
value.emplaceValue(5, 'b');
136+
121137
// or create value and pass it to async value
122138
auto str = std::make_unique<std::string>(5, 'b');
123139
value.moveValue(std::move(str));
124140
```
125-
User can assign error in a similar way:
141+
User can assign an error in a similar way:
126142
```C++
127143
AsyncValue<std::string> value(...);
128144

129145
// create error by passing error's constructor parameters
130146
value.emplaceError("Some error hapenned");
147+
131148
// or create error and pass it to async value
132149
auto err = std::make_unique<AsyncError>("Some error hapenned");
133150
value.moveError(std::move(err));

0 commit comments

Comments
 (0)