image - QML:在 GridView 中加载图像
问题描述
我是 QML 初学者,想在我的应用程序中加载一些图片。FileDialog
我选择包含大约 1000 张图像的文件夹。
然后,我想将它们加载到GridView
. ASwipeView
有助于将图像拆分为 40 个图像/屏幕。所以SwipeView
有25页。
现在如何在不等待 1 小时加载图像的情况下加载图像?
这是我的代码:
import QtQuick 2.6
import QtQuick.Controls 2.1
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import Qt.labs.folderlistmodel 1.0
import QtQuick.Controls 1.4
import QtQuick.Controls 2.1
import QtQuick.Dialogs 1.1
import QtQml.Models 2.1
Window{
visible: true
width: 1000
height: 600
FolderListModel{
id: lm
showDirs: false
}
FileDialog {
id: fileDialog
selectFolder: true
title: "Please choose a folder"
folder: shortcuts.home
onAccepted: {
lm.folder = fileUrl+"/"
}
onRejected: {
console.log("Canceled")
Qt.quit()
}
Component.onCompleted: visible = true
}
SwipeView {
width: 800
height: 500
clip: true
currentIndex: 0
Repeater {
model: Math.ceil(lm.count / 40)
delegate: gridView
}
}
Component{
id: gridView
GridView{
interactive: false
width: 800
height: 500
property int viewIndex: index
model: DelegateModel {
model: lm
groups: DelegateModelGroup { name: 'filter' }
Component.onCompleted: {
for (var i = viewIndex * 40; i < lm.count && i < (viewIndex * 40) + 40; i++) {
items.setGroups(i, 1, ['items', 'filter'])
}
}
filterOnGroup: 'filter'
delegate: Image {
width: 80
height: 120
source: lm.folder+fileName
asynchronous: true
}
}
}
}
}
如果有人可以帮助我,我会很高兴。
谢谢并恭祝安康,
埃迪
解决方案
问题是您正在加载所有 1000 张图像,而不仅仅是当前页面。
作为一个快速的解决方案,我建议您将 a 定义filterOnGroup
为'filter'
仅GridView
在当前页面上时,例如:
// note: `swipeViewId` is an id of the SwipeView
filterOnGroup: swipeViewId.currentIndex == index ? 'filter' : ''
另一种方法是Loader
用作委托,如果它是当前SwipeView
的,则将其设置sourceComponent
为,gridView
否则设置为 null。
Repeater {
model: Math.ceil(lm.count / 40)
delegate: Loader {
sourceComponent: SwipeView.isCurrentItem ? gridView : null
onLoaded: {
item.viewIndex = index
}
}
}
此外,如果实际图像大于其预览(在您的示例中为 80x120),您可以使用sourceSize属性Image
推荐阅读
- html - 找不到我网站右侧的多余空间来自哪里
- swift - 如何通过前缀匹配 coreData 中的搜索字符串对 fetchRequest 进行排序
- python - 当网址随时间变化时从网络下载文件
- php - 如何在自定义页面上显示消息 wc_print_notice
- xslt - 从字符串中删除内联 XML 标记而不添加空格
- python - 电报机器人中转发消息方法的问题
- javascript - 如何在 Angular 8 的 Cordova 函数中调用另一个函数?
- qt - 如何为 Visual Studio 项目构建 Qmake?专门用于 octomap 库的 QGLViewer
- laravel-6 - 检查日期是否不重叠
- linux - 用于运行命令并显示控制台输出的 Bash 脚本,直到出现某些短语或超时