设置加载器项目属性 [英] Set Loader item property
问题描述
在文件管理器的上下文中,我有一个TableView
组件保存在dirview.qml
文件中,使用FolderListModel
显示某个目录的内容:
In the context of a file manager, I have a TableView
component saved in the file dirview.qml
, which displays the content of some directory using FolderListModel
:
import QtQuick 2.4
import QtQuick.Controls 1.4
import Qt.labs.folderlistmodel 2.1
TableView {
id: tableView
property string folder_url: "file:///tmp"
anchors.fill: parent
TableViewColumn {
role: "fileName"
title: qsTr("Name")
width: tableView.width * 0.7
}
TableViewColumn {
role: "fileURL"
title: qsTr("Size")
width: tableView.width * 0.2
}
FolderListModel {
id: folderModel
folder: folder_url
nameFilters: ["*"]
showHidden: true
showDirsFirst: true
showDotAndDotDot: true
}
model: folderModel
}
现在在 main.qml
中,我想在 Tab
中加载这个组件.由于 Tab
是一个 Loader
,我可以执行以下操作:
Now in main.qml
, I want to load this component in a Tab
. Since Tab
is a Loader
, I can do the following:
TabView {
id: tabView2
Tab {
title: qsTr("Home")
source: "dirview.qml"
}
}
但是,我需要做的是以某种方式传递一个 URL 字符串以设置 dirview.qml
组件的 folder_url
属性,并且该字符串必须覆盖默认值 file:///tmp
(我说的是某种绑定").
However, what I need to do is somehow passing a URL string in order to set the folder_url
property of the dirview.qml
component, and that string must overwrite the default value file:///tmp
(so some kind of "binding" I'm talking about).
我尝试了一些无效的方法,例如使用 Tab.onLoaded
设置 item.folder_url = "file:///home"
.视图不会改变.
I tried a few things that didn't work, such as using Tab.onLoaded
to set item.folder_url = "file:///home"
. The view doesn't change.
正确的做法是什么?
谢谢!
推荐答案
您可以使用 Loader.item
属性来访问 Tab
对象.根据文档:
You can use Loader.item
property to access Tab
object. According to the documentation:
该属性保存当前加载的顶级对象
This property holds the top-level object that is currently loaded
例如:
Tab {
title: qsTr("Home")
source: "dirview.qml"
onLoaded: {
item.folder_url = "file:/home"
}
}
这篇关于设置加载器项目属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!