英文:
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<QtObject> content
ColumnLayout
{
readonly property list<QtObject> 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<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;
}
}
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<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"
}
}
答案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: "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" }
}
}
You can Try this Online!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论