Set Loader item property

孤街浪徒 提交于 2020-01-04 02:04:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!