在 TableView 中委托 FolderListModel [英] Delegate FolderListModel in TableView
问题描述
我正在学习 Qt Quick 来制作文件管理器,但我一般没有 QML 或 GUI 方面的经验.第一步是使用FolderListModel
列出文件夹的内容.我得到了 示例代码 使用 ListView
,但自然我想显示除了名称之外的多个字段,例如大小、时间等.因此,我正在考虑使用 TableView
.
I'm learning Qt Quick to make a file manager, but I have no experience in QML or GUI in general. The first step is to list the content of a folder, using FolderListModel
. I got the example code working using ListView
, but naturally I want to display multiple fields in addition to the name, e.g. size, time, and so on. Thus, I'm thinking of using TableView
.
但是,我不清楚如何将每个条目委派为 TableView
中的一行.目前我只是使用itemDelegate
来显示fileName
,结果是在每一行中,所有列都重复条目的名称.所以我认为 rowDelegate
是正确的方法,但我如何为此目的制作适当的委托 Component
?从概念上讲,我想指定一个字段数组,例如[model.fileName, model.fileSize]
对应表列.这可以实现吗?
However, it's not clear to me how to delegate each entry as a row in TableView
. Currently I'm simply using itemDelegate
to display fileName
, and the resultant is that in each row, all the columns repeat the name of the entry. So I think rowDelegate
is the correct way, but how do I make a proper delegate Component
for that purpose? Conceptually I'd like to specify an array of fields, e.g. [model.fileName, model.fileSize]
corresponding to the table columns. Is this achievable?
为了澄清起见,我发布了以下代码:
For clarify I'm posting the code below:
import QtQuick 2.4
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1
import Qt.labs.folderlistmodel 2.1
ApplicationWindow {
visible: true
width: 900
height: 600
title: qsTr("Hello World")
Item {
anchors.fill: parent
width: 900
height: 600
SplitView {
id: splitView1
anchors.fill: parent
TabView {
id: tabView1
width: splitView1.width / 2
Tab {
title: qsTr("Home")
TableView {
id: tableView1
width: splitView1.width / 2
TableViewColumn {
role: "name"
title: qsTr("Name")
width: tableView1.width * 0.75
}
TableViewColumn {
role: "size"
title: qsTr("Size")
width: tableView1.width * 0.25
}
FolderListModel {
id: folderModel2
folder: "file:/home/username"
nameFilters: ["*"]
showHidden: true
}
Component {
id: fileDelegate2
Text {
text: model.fileName
}
}
model: folderModel2
itemDelegate: fileDelegate2
}
}
}
}
}
}
推荐答案
文档 提到以下角色可用:
- 列表项
- 文件名
- 文件路径
- fileURL(自 Qt 5.2 起)
- 文件库名称
- 文件后缀
- 文件大小
- 修改文件
- 已访问文件
- fileIsDir
因此您不需要使用自定义委托来显示该信息,只需适当设置 TableViewColumn
的 role
属性:
So you don't need to have custom delegates to display that information, just set the role
property of TableViewColumn
appropriately:
import QtQuick 2.4
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1
import Qt.labs.folderlistmodel 2.1
ApplicationWindow {
visible: true
width: 900
height: 600
title: qsTr("Hello World")
Item {
anchors.fill: parent
width: 900
height: 600
SplitView {
id: splitView1
anchors.fill: parent
TabView {
id: tabView1
width: splitView1.width / 2
Tab {
title: qsTr("Home")
TableView {
id: tableView1
width: splitView1.width / 2
TableViewColumn {
role: "fileName"
title: qsTr("Name")
width: tableView1.width * 0.75
}
TableViewColumn {
role: "fileSize"
title: qsTr("Size")
width: tableView1.width * 0.25
}
FolderListModel {
id: folderModel2
folder: "file:/home/username"
nameFilters: ["*"]
showHidden: true
}
model: folderModel2
}
}
}
}
}
}
这篇关于在 TableView 中委托 FolderListModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!