QML连接具有静态条目的`QtObject`列表。

huangapple go评论58阅读模式
英文:

QML concat list<QtObject> with static entries

问题

我想创建一个带有ColumnLayout的QML组件,其中包含静态项和作为参数获取的项,类似于以下内容:

default property list<QtObject> content

ColumnLayout
{
  readonly property list<QtObject> defaultData: [
    Item { },
    Item { }
  ]
  data: defaultData.concat(content)
}

但是qml列表类型不实现concat。

目前我有这个解决办法:

default property list<QtObject> content

ColumnLayout
{
  readonly property list<QtObject> defaultData: [
    Item { },
    Item { }
  ]
  data: getData();

  function getData() {
    const result = defaultData;
    for (let i = 0; i < content.length; i++) result.push(content[i]);
    return result;
  }
}

尽管这个方法有效,但它会对data产生属性绑定循环错误,而且相当不美观。当然,可以嵌套两个ColumnLayouts,但是否有更好的解决方案?

英文:

I want to create a QML component with a ColumnLayout that consists of static items and items aquired as a parameter, something like this:

default property list&lt;QtObject&gt; content

ColumnLayout
{
  readonly property list&lt;QtObject&gt; defaultData: [
    Item { },
    Item { }
  ]
  data: defaultData.concat(content)
}

, but the qml list type does not implement concat.

Currently I have this workaround solution:

default property list&lt;QtObject&gt; content

ColumnLayout
{
  readonly property list&lt;QtObject&gt; defaultData: [
    Item { },
    Item { }
  ]
  data: getData();

  function getData() {
    const result = defaultData;
    for (let i = 0; i &lt; content.length; i++) result.push(content[i]);
    return result;
  }
}

but although this works this throws property binding loop errors for data and is quite ugly. Of course nesting two ColumnLayouts is possible, but is there a better solution?

答案1

得分: 1

你可以使用JS 扩展语法 来实现:

component MyItem: ColumnLayout {
    default property list<QtObject> content
    readonly property list<QtObject> defaultData: [
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 30
            border.width: 1
        },
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 30
            border.width: 1
        }
    ]
    data: [...defaultData, ...content]
}

MyItem {
    anchors.centerIn: parent
    width: 400

    Rectangle {
        Layout.fillWidth: true
        Layout.preferredHeight: 30
        color: "grey"
    }
}
英文:

You can use the JS spread syntax for that:

component MyItem: ColumnLayout {
    default property list&lt;QtObject&gt; content
    readonly property list&lt;QtObject&gt; defaultData: [
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 30
            border.width: 1
        },
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 30
            border.width: 1
        }
    ]
    data: [...defaultData, ...content]
}

MyItem {
    anchors.centerIn: parent
    width: 400

    Rectangle {
        Layout.fillWidth: true
        Layout.preferredHeight: 30
        color: &quot;grey&quot;
    }
}

答案2

得分: 0

尽管它可能不完全符合您的要求,但通过使用 Page 头部,您可以在用户组件之前插入您的静态组件。在以下示例中,我们实现了一个组件,插入了“A”和“B”组件,用户提供了“C”、“D”和“E”组件:

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
    Sample {
        ColumnLayout {
            TextField { text: "C" }
            TextField { text: "D" }
            TextField { text: "E" }
        }
    }
}

// Sample.qml
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
     header: ColumnLayout {
        width: parent.width
        TextField { text: "A" }
        TextField { text: "B" }
     }
}

您可以在此在线尝试!

英文:

Though, it may not be exactly what you want, making use of Page header you can insert your static components before a user's components. In the following example, we implement a component that inserts "A" and "B" components, and the user supplies "C", "D" and "E" components:

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
    Sample {
        ColumnLayout {
            TextField { text: &quot;C&quot; }
            TextField { text: &quot;D&quot; }
            TextField { text: &quot;E&quot; }
        }
    }
}

// Sample.qml
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
     header: ColumnLayout {
        width: parent.width
        TextField { text: &quot;A&quot; }
        TextField { text: &quot;B&quot; }
     }
}

You can Try this Online!

huangapple
  • 本文由 发表于 2023年3月7日 17:06:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定