gpt4 book ai didi

qt - BusyIndi​​cator 不显示

转载 作者:行者123 更新时间:2023-12-02 20:59:37 33 4
gpt4 key购买 nike

我想在一个漫长的过程正在进行时显示一个BusyIndi​​cator。问题是当我让它运行时它不会显示,并在该过程完成后显示。根据文档

The busy indicator should be used to indicate activity while content is being loaded or the UI is blocked waiting for a resource to become available.

我创建了一个基于原始代码的最小代码

Window {
id: win
width: 300
height: 300

property bool run : false

Rectangle {
anchors.fill: parent
BusyIndicator {
anchors.centerIn: parent
running: run
}

MouseArea {
anchors.fill: parent
onClicked: {
run = true
for(var a=0;a<1000000;a++) { console.log(a) }
run = false
}
}
}
}

因此,当单击矩形时,我想显示BusyIndi​​cator直到计算完成。

出于示例目的,我在这里使用了 for 循环。在实际场景中,我通过 ContextProperty 调用一个函数(将大约 1000 行插入数据库)。但在这种情况下,BusyIndi​​cator 也不会显示。

我的做法正确吗?或者最好的方法是什么?

最佳答案

您无法查看您的 BusyIndi​​cator,只是因为 onClicked 处理程序中的长时间操作会阻止应用程序 GUI 并且指示器不会更新。您应该在不同的线程中运行此类操作,以避免 GUI 卡住。简单的例子:

QML

Window {
id: win
width: 300
height: 300

property bool run : false

Rectangle {
anchors.fill: parent
BusyIndicator {
id: busy
anchors.centerIn: parent
running: win.run
}

MouseArea {
anchors.fill: parent
onClicked: {
win.run = true
thread.sendMessage({run : true});
}
}

WorkerScript {
id: thread
source: "handler.js"
onMessage: {
win.run = messageObject.run;
}
}
}
}

handle.js

WorkerScript.onMessage = function(message) {
if(message.run === true) {
for(var a=0;a<1000000;a++) { console.log(a) }
}
WorkerScript.sendMessage({run : false});
}

关于qt - BusyIndi​​cator 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27222247/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com