mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-02-02 19:27:42 +08:00
7f9013a878
- Add a root CMakeLists.txt defining an INTERFACE target. - Add CMake, qpm, Git and download installation instructions. - Add CMake and qmake integration instructions. - Remove smooth scroll when navigating the docs. - Add links validation to `npm run docs:link`. - Reorganize the sidebar API Reference section.
54 lines
2.0 KiB
Markdown
54 lines
2.0 KiB
Markdown
---
|
|
title: connect
|
|
---
|
|
|
|
# QtPromise::connect
|
|
|
|
*Since: 0.5.0*
|
|
|
|
```cpp
|
|
(1) QtPromise::connect(QObject* sender, Signal(T) resolver) -> QPromise<T>
|
|
(2) QtPromise::connect(QObject* sender, Signal(T) resolver, Signal(R) rejecter) -> QPromise<T>
|
|
(3) QtPromise::connect(QObject* sender, Signal(T) resolver, QObject* sender2, Signal(R) rejecter) -> QPromise<T>
|
|
```
|
|
|
|
Creates a `QPromise<T>` that will be fulfilled with the `resolver` signal's first argument, or a
|
|
`QPromise<void>` if `resolver` doesn't provide any argument.
|
|
|
|
The second `(2)` and third `(3)` variants of this method will reject the `output` promise when the
|
|
`rejecter` signal is emitted. The rejection reason is the value of the `rejecter` signal's first
|
|
argument or [`QPromiseUndefinedException`](../exceptions/undefined.md) if `rejected` doesn't provide
|
|
any argument.
|
|
|
|
Additionally, the `output` promise will be automatically rejected with [`QPromiseContextException`](../exceptions/context.md)
|
|
if `sender` is destroyed before the promise is resolved (that doesn't apply to `sender2`).
|
|
|
|
```cpp
|
|
class Sender : public QObject
|
|
{
|
|
Q_SIGNALS:
|
|
void finished(const QByteArray&);
|
|
void error(ErrorCode);
|
|
};
|
|
|
|
auto sender = new Sender();
|
|
auto output = QtPromise::connect(sender, &Sender::finished, &Sender::error);
|
|
|
|
// 'output' resolves as soon as one of the following events happens:
|
|
// - the 'sender' object is destroyed, the promise is rejected
|
|
// - the 'finished' signal is emitted, the promise is fulfilled
|
|
// - the 'error' signal is emitted, the promise is rejected
|
|
|
|
// 'output' type: QPromise<QByteArray>
|
|
output.then([](const QByteArray& res) {
|
|
// 'res' is the first argument of the 'finished' signal.
|
|
}).fail([](ErrorCode error) {
|
|
// 'error' is the first argument of the 'error' signal.
|
|
}).fail([](const QPromiseContextException& error) {
|
|
// the 'sender' object has been destroyed before any of
|
|
// the 'finished' or 'error' signals have been emitted.
|
|
});
|
|
```
|
|
|
|
See also the [`Qt Signals`](../qtsignals.md) section for more examples.
|