Convert the resolved value type of a QPromise
The convert<U>() method changes the resolved value of a QPromise<T> to type U. The conversion mechanism depends on the types involved:
- Fundamental Types/Enums: Performs a
static_cast<U>. - Converting Constructors: If
Uhas a non-explicit constructor accepting a single argument of typeT, that constructor is used. - QVariant Conversion: If the promise is
QPromise<QVariant>, it uses theQVariantconversion algorithm. If the conversion fails, the promise is rejected with aQPromiseConversionException. - General Case: Other combinations are converted via
QVariant. Custom types must be registered withQ_DECLARE_METATYPEand may require a registered converter viaQMetaType::registerConverterto avoidQPromiseConversionException.
Special Cases:
- If
Uisvoid, the resolved value is dropped. - Calling
converton aQPromise<void>is not supported.
// Example 1: Fundamental type conversion (int -> double)
QPromise<int> input = ...;
auto output = input.convert<double>();
output.then([](double value) { /* value is static_cast<double> */ });
// Example 2: Converting constructor (QByteArray -> QString)
QPromise<QByteArray> input = ...;
auto output = input.convert<QString>();
output.then([](const QString& value) { /* calls QString(QByteArray) */ });
// Example 3: QVariant conversion
QPromise<QVariant> input = ...;
auto output = input.convert<int>();
output.then([](int value) { /* success */ })
.fail([](const QPromiseConversionException& e) { /* conversion failed */ });