Qt 6中的异步操作
2023-06-13 09:17:28 时间
❝从Qt官网看到的一篇关于Qt 6的文章,分享给大家。❞
我们先看看Qt 6版本以前「从网络中加载图片的一般操作步骤」。
- 发出网络请求并等待,直到收到所有图像数据。
- 根据原始数据创建图像源。
- 处理图像。
- 显示图像。
具体的函数操作:
QByteArray download(const QUrl &url);
QImage createImage(const QByteArray &data);
QImage processImage(const QImage &image);
void show(const QImage &image);
代码实现:
void loadImage(const QUrl &url) {
QFuture data = QtConcurrent::run(download, url);
QFutureWatcher dataWatcher;
dataWatcher.setFuture(data);
connect(&dataWatcher, &QFutureWatcher ::finished, this, [=] {
// handle possible errors
// ...
QImage image = createImage(data);
// Process the image
// ...
QFuture processedImage = QtConcurrent::run(processImage, image);
QFutureWatcher<QImage> imageWatcher;
imageWatcher.setFuture(processedImage);
connect(&imageWatcher, &QFutureWatcher::finished, this, [=] {
// handle possible errors
// ...
show(processedImage);
});
});
}
Qt 6版本中可以这样操作。看起来是不是简便很多呢。
auto future = QtConcurrent::run(download, url)
.then(createImage)
.then(processImage)
.then(show)
.onFailed([](QNetworkReply::NetworkError) { // 错误处理
// handle network errors
})
.onFailed([](ImageProcessingError) { // 错误处理
// handle image processing errors
})
.onFailed([] { // 错误处理
// handle any other error
});
- 链接: https://www.qt.io/blog/asynchronous-apis-in-qt-6
相关文章
- qt 当前窗口句柄_QT获取窗口句柄
- qt串口通信接收数据不完整_qt串口接收数据
- QT多线程实战_Qt多线程开发项目
- Qt-网络编程
- 分享一个Qt安卓的docker配置
- Ubuntu18.04下Qt出现qt.qpa.plugin:Could not load the Qt platform plugin “xcb“问题解决方法
- 使用 Qt 连接远程 MySQL 数据库(qt连接远程mysql)
- Qt在Linux上的移植实践(qt移植到linux)
- Qt迭代器(Java类型和STL类型)详解
- 使用Qt框架轻松连接MySQL数据库(qt与mysql连接)
- 开发Linux Qt界面开发:拥抱开源世界的应用之旅(linuxqt界面)
- 技术深入探索Linux下的Qt开发(基于linux的qt开发)
- 在Linux下快速安装 Qt 5.7(linux安装qt5.7)
- Linux虚拟机中安装Qt的指南(linux虚拟机安装qt)
- 开发Linux下 Qt开发:从入门到精通.(linux下的qt)
- 快速搭建Qt环境中的MySQL数据库(qt配置mysql数据库)
- Qt编程实现MySQL数据库调用(qt调用MySQL数据库)
- Adept Qt to Access Linux Commands(qt调用linux命令)
- 快速使用QT进行MySQL数据库操作(qt使用mysql数据库)
- 使用Qt在Linux上实现软键盘(qtlinux软键盘)
- Qt技术与SQLServer的完美结合(qt与sqlserver)
- 界面让Linux拥有Qt完美图形界面的魅力(linux qt 图形)