【问题标题】:QML file include - or one monolithic file (structure QML code)?QML 文件包括 - 还是一个整体文件(结构 QML 代码)?
【发布时间】:2013-11-01 17:03:10
【问题描述】:

这是一个 QML 新手问题。从table view example 我有这样的代码:

Column {
        anchors.top: toolbar.bottom
        .....

        TabView {
            id:frame
            ......

            Tab {
                title: "XmlListModel"
                ...
            }
            Tab { ...

由于 qml 文件很长,我想知道我是否可以嵌套 qml 文件

Column {
        anchors.top: toolbar.bottom
        .....

        TabView {
            id:frame
            ......

            <include tab 1 qml file>   <-- possible ????? -------
            <include tab 2 qml file>

如果这样的include 是不可能的,QML 程序员如何构造他的代码?即使在simple example 中,也已经有太多行无法处理恕我直言。

-- 编辑--

回答后我发现这个值得阅读:

  1. http://qt-project.org/doc/qt-5.0/qtqml/qtqml-syntax-directoryimports.html
  2. How to reuse code at QML

【问题讨论】:

    标签: qml qt5.1


    【解决方案1】:

    不,你不能做“包含”,但你可以把东西放到命名对象中。

    例如,将您的 Tab #1 文件放入名为“Tab1”的文件中(或与其实际显示内容相关的更好名称;我不知道,所以无法为您提供名称) .

    所以在 Tab1.qml 中我们有:

    import ...
    Tab {
      id: tab1
      ...
    }
    

    然后你现在可以在主文件中引用它:

    ...
    Tabview {
       id: frame
       Tab1 { id: tab1 }
    }
    

    你会注意到我再次为它添加了一个 id,因为父级不会 能够在没有它的情况下引用孩子内部的 id。 (它们可以是不同的名称,但不要那样做。动物会哭。实际上,您也可以省略孩子中的 id,但很多人喜欢能够在文件中看到它。)

    【讨论】:

    • 如果没有,很可能是您没有看到的代码问题。也许您应该将其作为另一个问题发布?
    猜你喜欢
    • 2014-04-05
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2019-05-06
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多