当前位置:网站首页>Roson的Qt之旅#105 QML Image引用大尺寸图片
Roson的Qt之旅#105 QML Image引用大尺寸图片
2022-08-03 06:41:00 【Allen Roson】
1.Image控件是不是使用大图片时就报错?

如图,我准备了3个大尺寸的图片,想要在界面上显示出来,如是就把他们添加到了资源文件中:

然后编写qml代码:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Controls 1.4
Window {
id:myWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Image {
id: myImg1
width: myWindow.width
height: myWindow.height
source: "Resource/20211205_IMG_8727.JPG"
}
}
然后执行构建,会看到编译报错了:

导致这个错误的原因是:
添加的资源文件过大,会导致编译无法通过
2.如何解决cc1plus.exe:-1: error: out of memory allocating 1073745919 bytes?
有两种方法:
- 方法1:
将图片添加到qrc资源文件中,然后在工程配置文件也就是.pro文件中添加如下代码:
CONFIG += resources_big接下来就可以在qml中使用相对路径引用图片了。
- 方法2:
不要将尺寸大的资源文件(图片、音乐、视频)等添加到qrc资源文件中,使用绝对路径来引用图片
3.使用相对路径引用图片代码示例
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Controls 1.4
Window {
id:myWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Image {
id: myImg1
width: myWindow.width
height: myWindow.height
source: "Resource/20211205_IMG_8727.JPG"
}
}

4.使用绝对路径引用图片代码示例
先删除qrc中添加的大图片,并保存
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Controls 1.4
Window {
id:myWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
TabView {
id:myTab
width: myWindow.width
height: myWindow.height
Tab {
title: "Img1"
Image {
id: name1
width: myWindow.width
height: myWindow.height-50
source: "file:///E:/02_Code/gitee/RosonPlayer/Resource/20211205_IMG_8727.jpg"
}
}
Tab {
title: "Img2"
Image {
id: name2
width: myWindow.width
height: myWindow.height-50
source: "file:///E:/02_Code/gitee/RosonPlayer/Resource/20211205_IMG_8732.jpg"
}
}
Tab {
title: "Img3"
Image {
id: name3
width: myWindow.width
height: myWindow.height-50
source: "file:///E:/02_Code/gitee/RosonPlayer/Resource/20211205_IMG_8737.jpg"
}
}
}
}

注意:
编译版本选择Debug时,编译出来的程序,有1张图片显示不出来,换成release版本重新编译后,就正常了,原因没有深究,感觉像是QT的BUG.

边栏推荐
- 测试用例设计方法之因果图详解
- volatile
- 重量级大咖来袭:阿里云生命科学与智能计算峰会精彩内容剧透
- 关于利用canvas画带箭头的直线旋转
- El - tree to set focus on selected highlight highlighting, the selected node deepen background and change the font color, etc
- How to choose a reliable and formal training institution for the exam in September?
- 数据仓库指标体系实践
- consul理解
- 最新版图书馆招聘考试常考试题重点事业单位
- 深入理解IO流(第一篇)
猜你喜欢
随机推荐
安全狗云原生安全能力全面亮相全球数字经济大会暨ISC互联网安全大会
关于利用canvas画带箭头的直线旋转
MYSQL存储过程注释详解
ORB-SLAM2提取特征点
qt学习之旅--MinGW32编译opencv3.0.0
第四章:架构,Architecture
剑指offer专项突击版第18天
信息学奥赛一本通T1446:素数方阵
【多线程进阶】--- 常见锁策略,CAS,synchronized底层工作原理,JUC,线程安全的集合类,死锁
IEEE RAL投初稿
information_schema
consul理解
重量级大咖来袭:阿里云生命科学与智能计算峰会精彩内容剧透
PHP 获取服务器信息
现货黄金分析的主要流派
HCIP笔记整理 2022/7/20
信息学奥赛一本通T1451:棋盘游戏
boot - SSE
PMP每日一练 | 考试不迷路-8.2(包含敏捷+多选)
解读 refresh 十二步骤









