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

QML界面调用C++层阻塞函数,如何不卡界面

1.C++ 后端支持异步操作(这段需要注意)

// controller_module.h
#pragma once
#include <QObject>
#include <QtConcurrent>
#include <QFutureWatcher>class ControllerModule : public QObject
{Q_OBJECT
public:explicit ControllerModule(QObject *parent = nullptr) : QObject(parent) {}Q_INVOKABLE void setUseSolutionAsync(const QString &solution) {// 使用 QtConcurrent 在后台线程执行QFuture<void> future = QtConcurrent::run([this, solution]() {this->setUseSolution(solution); // 调用原来的同步方法});QFutureWatcher<void> *watcher = new QFutureWatcher<void>(this);connect(watcher, &QFutureWatcher<void>::finished, this, [this, watcher]() {emit setSolutionFinished();watcher->deleteLater();});watcher->setFuture(future);}// 原来的同步方法(会卡界面)void setUseSolution(const QString &solution) {// 模拟耗时操作QThread::sleep(3); // 假设这里是很耗时的操作qDebug() << "Solution set to:" << solution;// 实际的操作代码...}signals:void setSolutionStarted();void setSolutionFinished();void setSolutionError(const QString &error);
};

2.QML挡住主界面的提示框界面

// WaitDialog.qml
import QtQuick 2.15
import QtQuick.Controls 2.15Popup {id: waitDialogmodal: truedim: trueclosePolicy: Popup.NoAutoClosewidth: 200height: 120x: (parent.width - width) / 2y: (parent.height - height) / 2background: Rectangle {color: "white"radius: 8border.color: "#cccccc"}Column {anchors.centerIn: parentspacing: 15BusyIndicator {id: busyIndicatorrunning: trueanchors.horizontalCenter: parent.horizontalCenter}Text {text: "正在切换方案..."font.pixelSize: 14color: "#333333"}}
}

3.主界面,ComboBox 的切换调用C++层阻塞函数

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15Item {id: root// 等待对话框WaitDialog {id: solutionWaitDialog}ComboBox {id: solutionCombomodel: workParamsVM.solutionListLayout.fillWidth: truebackground: Rectangle {color: "#ADD8E6"radius: 3border.color: parent.down ? "#17a81a" : "#21be2b"}// 绑定到ViewModelcurrentIndex: model.indexOf(workParamsVM.solution)// 更新ViewModelonActivated: {var selectedSolution = currentText;// 先更新界面显示workParamsVM.solution = selectedSolution;// 显示等待对话框solutionWaitDialog.open();// 在后台线程执行耗时操作controllerModule.setUseSolutionAsync(selectedSolution);}}// 连接信号Connections {target: controllerModuleonSetSolutionFinished: {// 操作完成,关闭等待对话框solutionWaitDialog.close();}onSetSolutionError: {// 发生错误,关闭对话框并显示错误信息solutionWaitDialog.close();errorPopup.showError(error);}}
}
http://www.dtcms.com/a/389816.html

相关文章:

  • JVM GC 调优:GC 问题发现工具,五大 GC 异常模式,四大调优方案与案例实战
  • Excel处理控件Aspose.Cells教程:如何使用Python在Excel中创建下拉列表
  • React 18.2中使用Redux 5.0.1
  • 程序开发的基本规律
  • Day26_【深度学习(6)_神经网络NN(1.1)激活函数_softmax详解篇】
  • 通过调用deepseek大模型接口对千条评论信息进行文本分析/词频分析/情感分析
  • 攻坚家电代工转型痛点|远望电器牵手盘古信息,以IMS重塑数字制造根基
  • SpringBoot实现Markdown语法转HTML标签
  • DeepSeek:大语言模型在中文生态中的技术突破与应用探索
  • 【Agent博客分享】从多Agent问题到新的上下文工程方法
  • 点云分割中 offset 与 batch 表示的转换详解
  • C++23 堆栈跟踪功能实战:从内存泄漏梦魇到一键定位的调试革命
  • jvm参数调优(持续更新)
  • 容器查看日志工具-stern
  • 衍射光学元件DOE:台阶高度与位置误差的测量
  • Java中对象/嵌套对象属性复制工具类使用示例:Hutools工具类BeanUtils使用示例
  • rust编写web服务02-路由与请求处理
  • Spring Cloud - 微服务限流的方式
  • 【智能系统项目开发与学习记录】ROS2基础(1)
  • 人工智能面试题:什么是CRF条件随机场
  • [x-cmd] 命令式交互、CLI/TUI 设计与 LLM
  • 基于AMBA总线协议的Verilog语言模型实现
  • 【Agent项目复现】OpenManus复现
  • 高校AI虚拟仿真实训平台软件解决方案
  • Vue3 + Ant Design Vue 实现统一禁用样式管理方案,禁用状态下已有值颜色区分(CSS 变量方案)
  • Ubuntu 24.04部署MongoDB
  • 8.1-spring 事务-声明式事务(使用)
  • Vue3》》组件继承 extends
  • 无人系统在边境管控的应用探讨
  • 一个典型的mysql数据库连接池初始化函数