当前位置: 首页 > news >正文

【QML】QML多线程应用(WorkerScript)

1. 实现功能

QML项目中,点击一个按键后,运行一段比较耗时的程序,此时ui线程会卡住。如何避免ui线程卡住。

2. 单线程(会卡住)

2.1 界面

在这里插入图片描述

2.2 现象

  • 点击delay btn后,执行耗时函数(TestJs.func_delay())界面阻塞(阻塞了12s),等待运行完成后,点击ui btn才能响应。
qml: 2024-11-14 09:48:29 ui thread
qml: 2024-11-14 09:48:30 click delay btn
qml: delay thread 2024-11-14 09:48:42
qml: 2024-11-14 09:48:42 ui thread

2.3 main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import "test.js" as TestJsWindow {width: 640height: 480visible: truetitle: qsTr("Thread")Row{spacing: 20Button{width: 200height: 100text: "ui btn"onClicked: {console.log(Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"), "ui thread")}}Button{width: 200height: 100text: "delay btn"onClicked: {console.log(Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"), "click delay btn")TestJs.func_delay()}}}
}

2.4 test.js


//耗时函数
function func_delay() {var cnt = 0;for(let i=0; i<1000000000; i++){cnt++;}console.log("delay thread", Qt.formatDateTime(new Date(),"yyyy-MM-dd HH:mm:ss"))
}

3. 多线程(WorkerScript方式)

3.1 界面

在这里插入图片描述

3.2 现象

  • 将耗时函数(func_delay())放到WorkerScript.onMessage中执行,这样ui线程不会阻塞。
  • ui线程与delay线程直接可以通讯,发送通过sendMessage,接收通过onMessage
qml: 2024-11-14 09:54:11 ui thread
qml: 2024-11-14 09:54:11 click delay btn-1
qml: 2024-11-14 09:54:11 click delay btn-2
qml: 2024-11-14 09:54:12 ui thread
qml: 2024-11-14 09:54:13 ui thread
js: delay thread 2024-11-14 09:54:23
js: ui thread -> delay thread:  Hello, I am ui thread.
qml: delay thread -> ui thread:  Hello, I am delay thread.

3.3 main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import "test.js" as TestJsWindow {width: 640height: 480visible: truetitle: qsTr("Thread")Row{spacing: 20Button{width: 200height: 100text: "ui btn"onClicked: {console.log(Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"), "ui thread")}}Button{width: 200height: 100text: "delay btn"onClicked: {console.log(Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"), "click delay btn-1")//发送数据myWorker.sendMessage({'msg': 'Hello, I am ui thread.'})console.log(Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"), "click delay btn-2")}}WorkerScript{id: myWorkersource: "test.js"//接收数据onMessage: {console.log("delay thread -> ui thread: ", messageObject.msg)}}}
}

3.4 test.js


function func_delay() {var cnt = 0;for(let i=0; i<1000000000; i++){cnt++;}console.log("delay thread", Qt.formatDateTime(new Date(),"yyyy-MM-dd HH:mm:ss"))
}WorkerScript.onMessage = function(message){func_delay()console.log("ui thread -> delay thread: ", message.msg)//发送数据WorkerScript.sendMessage({'msg': 'Hello, I am delay thread.'})
}

参考

Qt 之 qml WorkerScript使用

http://www.lryc.cn/news/484690.html

相关文章:

  • 认证鉴权框架SpringSecurity-1--概念和原理篇
  • 计算器上的MC、MR、M+、M—、CE是什么意思?
  • 无人机飞手执照处处需要,森林、石油管道、电力巡检等各行业都需要
  • 计算机网络——路由选择算法
  • 【前端】技术演进发展简史
  • 深入解析贪心算法及其应用实例
  • 电子工牌独立双通道定向拾音方案(有视频演示)
  • 举例理解LSM-Tree,LSM-Tree和B+Tree的比较
  • React Native 全栈开发实战班 - 核心组件与导航
  • Leecode热题100-35.搜索插入位置
  • 密码学知识点整理二:常见的加密算法
  • Linux如何将文件或目录打成rpm包?-- rpmbuild打包详解
  • RabbitMQ-死信队列(golang)
  • 爬虫开发工具与环境搭建——环境配置
  • 15.UE5等级、经验、血条,魔法恢复和消耗制作
  • 【Homework】【5】Learning resources for DQ Robotics in MATLAB
  • vue3中 ref和reactive的区别
  • 第十四章 Spring之假如让你来写AOP——雏形篇
  • 群控系统服务端开发模式-应用开发-前端个人资料开发
  • 动态规划技巧点
  • 深度学习之pytorch常见的学习率绘制
  • Spring Boot集成SQL Server快速入门Demo
  • 低代码牵手 AI 接口:开启智能化开发新征程
  • 【已解决】git push一直提示输入用户名及密码、fatal: Could not read from remote repository的问题
  • python语言基础-4 常用模块-4.13 其他模块
  • 微信小程序=》基础=》常见问题=》性能总结
  • JWT深度解析:Java Web中的安全传输与身份验证
  • 使用Java爬虫获取商品订单详情:从API到数据存储
  • Mybatis中批量插入foreach优化
  • Word VBA如何间隔选中多个(非连续)段落