qt - 如何使可见属性在 QML 中立即起作用?
问题描述
演示:
Window {
visible: true
width: 640
height: 480
Component.onCompleted: {
test.visible = true // 1. Show rect
for(var i = 0; i < 5000000000; i++){var t = i * i} // 2. Slow process, Sleep here
}
Rectangle {
id: test
color: "red"
width: 100; height: 100
visible: false
}
}
功能完成时可见属性起作用。在演示中,test
矩形无法显示1.
,必须等到功能完成。
我知道这应该是由进程块渲染引起的。但是有什么技巧可以解决这个问题吗?
解决方案
繁重的任务不应在 GUI 线程中执行,而应在另一个线程中执行,以免它们被阻塞。QML 提供给 WorkerScript,这允许您在另一个线程中执行任务:
慢进程.js
WorkerScript.onMessage = function() {
for(var i = 0; i < 5000000000; i++){
var t = i * i
console.log(t)
}
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
Component.onCompleted: {
test.visible = true // 1. Show rect
ws.sendMessage()
}
WorkerScript {
id: ws
source: "slow_process.js"
}
Rectangle {
id: test
color: "red"
width: 100; height: 100
visible: false
}
}
推荐阅读
- haskell - 单子的这种概括有一个通用名称吗?
- mysql - 如何对“分组依据”子句进行排序,以便返回的值是最后一个
- python - 在 python 中仅提取 txt 文件的第二列的更好方法?
- javascript - React - 如何创建链式级联辅助函数
- android - RecyclerView:没有附加适配器;跳过布局没有数据显示
- instagram - 查看 Instagram 中是否存在帐户的最佳 API?
- sass - 错误:编译我自己的引导主题时,`mix($color-1, $color-2, $weight: 50%)` 的参数 `$color-2` 必须是颜色
- python - 在 Python 中使用 ctypes 访问 DLL 返回的对象的内容
- ruby-on-rails-5.1 - 提交输入错误后,f.submit 在返回表单时不起作用 Rails 5.1.6
- sql - 在 Access 查询中添加第三个加入时出现问题