问题
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
}
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"
}
}
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).
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.
What is the right way to do it?
Thanks!
回答1:
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
For example:
Tab {
title: qsTr("Home")
source: "dirview.qml"
onLoaded: {
item.folder_url = "file:/home"
}
}
来源:https://stackoverflow.com/questions/33536881/set-loader-item-property