qt - 在事件循环之后执行信号
问题描述
在我的 .qml 文件中,当Button
按下 a 时,它会发出一个信号,并采取一些行动。此操作需要很长时间(它正在加密数据),我希望按钮在此操作开始之前自行“取消推送”,因此我可以在顶部显示进度条。
我知道 Qt 有事件循环,不知何故,我需要在这个事件循环之后发出这个信号,或者当 GUI 完成所有对象的 Paint() 时,所以按钮显示为未按下。
这是怎么做到的?
这是在信号上执行的代码,繁重的过程是aewm_obj.save_account(key,account)
,在台式机上需要 2 秒,在 Android 上需要 10 秒:
function save_account() {
if (fld_password1.length===0) {
if (account.type==WalletAccount.WACCT_TYPE_FULL) {
return
}
}
if (fld_password1.text!==fld_password2.text) {
console.log("passwords do not match")
} else {
btn_save.enabled=true;
if (account.type==WalletAccount.WACCT_TYPE_FULL) {
key.password=fld_password1.text
account.password=fld_password1.text
}
btn_save.enabled=false
encrypting_message.open()
if (aewm_obj.save_account(key,account)) {
encrypting_message.close()
accounts_root.back_to_accounts()
} else {
encrypting_message.close()
}
}
}
问题是,带有进度条的对话框在繁重的过程完成后显示,但它之前被调用过。发生这种情况是因为显示和隐藏组件的事件循环处理得太晚了。
解决方案
使用超时为 0 的单次计时器来排队返回事件循环后将触发的事件。在 C++ 中,这通常看起来像:
QTimer::singleShot(0, [] { /* do something */ });
QML 等价物是:
Timer {
interval: 0; running: true; repeat: false
onTriggered: /* do something */
}
推荐阅读
- powershell - 如何正确测试模拟 CmdLet 的 ErrorVariable 值?
- regex - Nintex 正则表达式 - 匹配姓氏、名字
- c - 如何使用 PIC18f87k22 定时器作为中断
- java - 如果没有字符串池,String 对象如何工作?
- java - Java & Android:获取路由器的 MAC 地址/ip.vX 地址
- javascript - 聚合物纸工具提示
- android - ARCore 如何使用 Unreal 记录 Android 手机屏幕上的照片和视频并保存到手机?
- firefox - Gridalicious 库未在 Firefox 中以全屏宽度加载网格
- batch-file - Delete files based on partial names in a text file
- python - altair 分组条形图格式轴和填充