QML 中引用 js 文件闪退问题

ops/2024/10/21 3:45:49/

问题描述

在移植 Android 中遇到这样一个引用兼容性问题,起因是这样的,Windows 版本的采用了 QML 分离的方式加载,而 Android 版本又采用了 qrc 的方式。而 Qt 中的机制是采用 QML 分离方式时则使用相对路径的方式引用 js 文件,而采用 qrc 的方式则需要使用 qrc 的方式引用 js 文件。且两种方式不能同时使用。

在项目开发时使用了 Windows 下 QML 分离的方式加载,在 qml 文件中使用相对路径的方式引用了 js 文件,在 Android 环境下编译运行时程序闪退。

Windows 下引用方式

import "./js/ui.js" as UI

Android 下编译报错信息

无法找到 qrc:/qml/js/ui/js

验证过程

尝试过很多中方式来验证兼容性,最终找到一种比较巧妙的方式解决。

  1. 通过代码中平台判断区分,不可行,qml 头部区域不能使用判断语句
if(Qt.platform.os === "android")import "./js/ui.js" as UI
elseimport "qrc:/js/js/ui.js" as UI
  1. 引用两个 js,使用时分开,不可行,仍然会报错
import "./js/ui.js" as UI
import "qrc:/js/js/ui.js" as AUI// 使用时
Qt.platform.os === "android" ? AUI : UI

解决方案

在 qml 中仍然使用相对路径的方式,Qt Android 编译时自动转换为 qrc 方式加载。

根据 QT 编译时报错的信息 qrc:/qml/js/ui/js 可以判断系统将相对路径自动转换为了 qrc 的方式,所以只要正好使用相对路径时并转换为 qrc 的方式也可以使用。

通过分析发现,可以直接 qml.qrc 中引用的路径,将原来的 <qresource prefix="/qml"> 改为 <qresource prefix="/">,并且将 <qresource prefix="/js"> 下的 js 文件移动到 <qresource prefix="/"> 下,这样正好相对路径转换为 qrc 方式后可用。

修改前:

<RCC><qresource prefix="/qml"><file>main.qml</file><file>TitleBar.qml</file><file>CenterMain.qml</file><file>home/MachineStatus.qml</file><file>base/DCChisel.qml</file><file>base/DCPan.qml</file><file>base/FoldButton.qml</file><file>base/LedLight.qml</file><file>base/LedTag.qml</file><file>base/LineInfo.qml</file><file>base/RisableInput.qml</file><file>base/StatusIndicator.qml</file><file>base/TComboBox.qml</file><file>base/TElseTheme.qml</file><file>base/ToolTitle.qml</file><file>base/TSelector.qml</file><file>base/TSpinBox.qml</file><file>base/TWaitBox.qml</file><file>dialog/DateSelect.qml</file><file>dialog/LicenseConfirm.qml</file><file>dialog/InfoConfirm.qml</file><file>dialog/PasswordInput.qml</file><file>home/CncProgress.qml</file><file>home/NcInfo.qml</file><file>home/NcImage.qml</file><file>home/CncBar.qml</file><file>tool/ProcessPage.qml</file><file>tool/WorkMode.qml</file><file>tool/Setting.qml</file><file>tool/Monitor.qml</file><file>tool/Update.qml</file><file>tool/Maintain.qml</file><file>tool/mode/Offline.qml</file><file>tool/mode/LinkLogin.qml</file><file>tool/mode/AreaNet.qml</file><file>tool/mode/CloudDisk.qml</file><file>tool/mode/FabricateChisel.qml</file><file>tool/mode/FabricatePan.qml</file><file>tool/update/Lifecycle.qml</file><file>tool/update/fwUpdate.qml</file><file>tool/update/DCUpdate.qml</file><file>tool/update/SpindleUpdate.qml</file><file>DeviceList.qml</file><file>tool/setting/BusinessInfo.qml</file><file>tool/setting/CalibertionRecord.qml</file><file>tool/setting/CommandSettings.qml</file><file>tool/setting/MachineConfig.qml</file><file>tool/setting/ManualOperation.qml</file><file>tool/setting/ProcessParameter.qml</file><file>tool/setting/ResetToFactory.qml</file><file>tool/setting/ServoOperation.qml</file><file>tool/setting/SpindleExportation.qml</file><file>tool/setting/ToolAxisesCalc.qml</file><file>tool/setting/ToolPosition.qml</file><file>tool/setting/UpdateRecord.qml</file><file>home/Device.qml</file><file>tool/maintain/Calibration.qml</file><file>tool/maintain/DeviceInformation.qml</file><file>tool/maintain/ToolRecord.qml</file><file>tool/maintain/ToolStateBar.qml</file><file>tool/maintain/ToolControl.qml</file><file>tool/maintain/LogExport.qml</file><file>tool/maintain/HistroyRecord.qml</file><file>tool/maintain/ParamExportImport.qml</file><file>tool/maintain/SDCardConfig.qml</file><file>tool/maintain/BreakPointProcess.qml</file><file>home/MessageQueue.qml</file><file>tool/maintain/AutoReflueling.qml</file><file>tool/maintain/SpindleMT.qml</file><file>tool/maintain/WorkspaceMT.qml</file><file>tool/update/Panel_list.qml</file><file>tool/maintain/ToolConfig.qml</file><file>help/HelpPage.qml</file><file>help/Language.qml</file><file>help/CncUpdate.qml</file><file>help/Service.qml</file><file>OutageContine.qml</file><file>tool/DCList.qml</file><file>tool/maintain/ExitAffirm.qml</file><file>ContinueProcess.qml</file><file>tool/setting/RDCDebug.qml</file><file>tool/setting/rdc/RDCControl.qml</file><file>tool/setting/rdc/RDCParam.qml</file><file>base/TCell.qml</file><file>base/TMenuItem.qml</file></qresource><qresource prefix="/js"><file>js/cncParam.js</file><file>js/machineParam.js</file><file>js/func.js</file><file>js/servoParam.js</file><file>js/ui.js</file></qresource><qresource prefix="/"/>
</RCC>

修改后:

<RCC><qresource prefix="/"><file>main.qml</file><file>TitleBar.qml</file><file>CenterMain.qml</file><file>home/MachineStatus.qml</file><file>base/DCChisel.qml</file><file>base/DCPan.qml</file><file>base/FoldButton.qml</file><file>base/LedLight.qml</file><file>base/LedTag.qml</file><file>base/LineInfo.qml</file><file>base/RisableInput.qml</file><file>base/StatusIndicator.qml</file><file>base/TComboBox.qml</file><file>base/TElseTheme.qml</file><file>base/ToolTitle.qml</file><file>base/TSelector.qml</file><file>base/TSpinBox.qml</file><file>base/TWaitBox.qml</file><file>dialog/DateSelect.qml</file><file>dialog/LicenseConfirm.qml</file><file>dialog/InfoConfirm.qml</file><file>dialog/PasswordInput.qml</file><file>home/CncProgress.qml</file><file>home/NcInfo.qml</file><file>home/NcImage.qml</file><file>home/CncBar.qml</file><file>tool/ProcessPage.qml</file><file>tool/WorkMode.qml</file><file>tool/Setting.qml</file><file>tool/Monitor.qml</file><file>tool/Update.qml</file><file>tool/Maintain.qml</file><file>tool/mode/Offline.qml</file><file>tool/mode/LinkLogin.qml</file><file>tool/mode/AreaNet.qml</file><file>tool/mode/CloudDisk.qml</file><file>tool/mode/FabricateChisel.qml</file><file>tool/mode/FabricatePan.qml</file><file>tool/update/Lifecycle.qml</file><file>tool/update/fwUpdate.qml</file><file>tool/update/DCUpdate.qml</file><file>tool/update/SpindleUpdate.qml</file><file>DeviceList.qml</file><file>tool/setting/BusinessInfo.qml</file><file>tool/setting/CalibertionRecord.qml</file><file>tool/setting/CommandSettings.qml</file><file>tool/setting/MachineConfig.qml</file><file>tool/setting/ManualOperation.qml</file><file>tool/setting/ProcessParameter.qml</file><file>tool/setting/ResetToFactory.qml</file><file>tool/setting/ServoOperation.qml</file><file>tool/setting/SpindleExportation.qml</file><file>tool/setting/ToolAxisesCalc.qml</file><file>tool/setting/ToolPosition.qml</file><file>tool/setting/UpdateRecord.qml</file><file>home/Device.qml</file><file>tool/maintain/Calibration.qml</file><file>tool/maintain/DeviceInformation.qml</file><file>tool/maintain/ToolRecord.qml</file><file>tool/maintain/ToolStateBar.qml</file><file>tool/maintain/ToolControl.qml</file><file>tool/maintain/LogExport.qml</file><file>tool/maintain/HistroyRecord.qml</file><file>tool/maintain/ParamExportImport.qml</file><file>tool/maintain/SDCardConfig.qml</file><file>tool/maintain/BreakPointProcess.qml</file><file>home/MessageQueue.qml</file><file>tool/maintain/AutoReflueling.qml</file><file>tool/maintain/SpindleMT.qml</file><file>tool/maintain/WorkspaceMT.qml</file><file>tool/update/Panel_list.qml</file><file>tool/maintain/ToolConfig.qml</file><file>help/HelpPage.qml</file><file>help/Language.qml</file><file>help/CncUpdate.qml</file><file>help/Service.qml</file><file>OutageContine.qml</file><file>tool/DCList.qml</file><file>tool/maintain/ExitAffirm.qml</file><file>ContinueProcess.qml</file><file>tool/setting/RDCDebug.qml</file><file>tool/setting/rdc/RDCControl.qml</file><file>tool/setting/rdc/RDCParam.qml</file><file>base/TCell.qml</file><file>base/TMenuItem.qml</file><file>js/cncParam.js</file><file>js/machineParam.js</file><file>js/func.js</file><file>js/servoParam.js</file><file>js/ui.js</file></qresource>
</RCC>

http://www.ppmy.cn/ops/4879.html

相关文章

购物车实现

目录 1.购物车常见的实现方式 2.购物车数据结构介绍 3.实例分析 1.controller层 2.service层 1.购物车常见的实现方式 方式一&#xff1a;存储到数据库 性能存在瓶颈方式二&#xff1a;前端本地存储 localstorage在浏览器中存储 key/value 对&#xff0c;没有过期时间。s…

计算机网络之同轴电缆,集线器,网桥,交换机,路由器

ping的过程 两台主机用交叉线连接&#xff0c;通过88.2ping88.3发现底层是先经过广播&#xff0c;通过arp协议&#xff0c;告诉我要找的ip是88.3,然后88.3主机收到后就把自己的mac地址发送回去&#xff0c;同理88.2发现是发给自己的后就进行接收&#xff0c;有了mac地址然后再通…

利用AQS(AbstractQueuedSynchronizer)实现一个线程同步器

目录 1. 前言 2. 什么是同步器 3. 同步器实现思路 Semaphore(信号量) 4. 代码实现 4.1. 创建互斥锁类 4.2 编写静态内部类&#xff0c;继承AQS 4.3 内部类实现AQS钩子函数 4.3 封装lock&#xff0c;unlock方法 4.4. 测试 5. 总结 本文章源码仓库&#xff1a;Conc…

javaWeb项目-智能仓储系统功能介绍

项目关键技术 开发工具&#xff1a;IDEA 、Eclipse 编程语言: Java 数据库: MySQL5.7 框架&#xff1a;ssm、Springboot 前端&#xff1a;Vue、ElementUI 关键技术&#xff1a;springboot、SSM、vue、MYSQL、MAVEN 数据库工具&#xff1a;Navicat、SQLyog 1、JSP技术 JSP(Jav…

混合现实(MR)开发框架

混合现实&#xff08;MR&#xff09;开发框架为开发者提供了构建MR应用程序所需的基本工具和功能。它们通常包括3D引擎、场景图、输入系统、音频系统、网络功能以及支持同时处理现实世界和虚拟世界信息的功能。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&…

虚拟机磁盘剩余空间不足

VMware 弹出提示&#xff1a; 对文件“E:\Virtual Machine\CentOS 7 1810 的克隆 (2)\CentOS 7 1810-cl1.vmdk”的操作失败。 如果该文件位于远程文件系统上&#xff0c;请确保网络连接以及该磁盘所在的服务器正常工作。如果该文件位于可移动介质中&#xff0c;请重新连接该介…

大数据平台搭建2024(一)

一&#xff1a;基础配置 创建虚拟机并查出ip地址进行连接 ip a1.配置node01静态ip地址与主机名 vi /etc/sysconfig/network-scripts/ifcfg-ens33修改或添加如下内容&#xff1a; BOOTPROTO"static" ONBOOTyes #根据虚拟机网卡信息配置 IPADDR192.168.200.141 NET…

C# 将 TextBox 绑定为 KindEditor 富文本

目录 关于 KindEditor 绑定设计 部署 KindEditor 实现代码 小结 关于 KindEditor KindEditor 基于JavaScript 编写&#xff0c;可以与众多WEB应用程序结合。KindEditor 依靠出色的用户体验和领先的技术提供富文本编辑功能&#xff0c;是一款非常受欢迎的HTML在线编辑器。…