То, что я хочу сделать, - это универсальная функция, которая получает информацию обо всех полях ввода (TextField, SpinBox, ComboBox, возможно, некоторые из них я подделываю)
Давайте представим эту форму:

Я хочу получить объект, который получает то, что там было заполнено.Он должен проверить, является ли это TextField
или ComboBox
, потому что способ получить информацию отличается.
Так что я хочу что-то вроде этого "мета-кода":
Object = {}
for(input in inputs) {
if(input.type=="TextField") {
Object.push(input.text)
} else if (input.type == "ComboBox") {
Object.push(input.currentText)
}
}
Я реализую кнопку или что-то, что вызовет функцию.
Чтобы сделать это немного сложнее, не все элементы формы будут на одном уровне, некоторые будут, например, дочерними элементами.
Ниже приведен код того, что я хочу сделать:
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
ApplicationWindow {
id: window
title: "Stack"
visible: true
width: 1400
Page {
id: page
anchors.fill: parent
property int responsiveWidth: 1000
property int maximumWidth: 900
ScrollView {
anchors.fill: parent
GridLayout {
columns: 2
width: page.width > page.responsiveWidth ? page.maximumWidth : page.width
anchors.top: parent.top
anchors.left: parent.left
anchors.leftMargin: page.width > page.responsiveWidth ? (page.width - childrenRect.width)/2 : 10
anchors.rightMargin: page.width > page.responsiveWidth ? 0 : 10
Button {
Layout.fillWidth: true
Layout.columnSpan: 2
text: "export"
onClicked: {
console.log("here i want to get an object with info related with the fields of these form")
}
}
Label {
text: "Company Name"
color: "red"
Layout.fillWidth: true
}
TextField {
objectName: "company_name"
font.bold: true
Layout.fillWidth: true
Layout.rightMargin: 10
}
Label {
text: "choices"
color: "red"
Layout.fillWidth: true
}
ComboBox {
Layout.fillWidth: true
model: [ "Banana", "Apple", "Coconut" ]
}
Item {
Layout.fillWidth: true
implicitHeight: 100
Layout.columnSpan: 2
Label {
anchors.left: parent.left
anchors.top: parent.top
text: "label"
color: "red"
width: parent.width
}
TextField {
anchors.left: parent.left
anchors.bottom: parent.bottom
objectName: "company_name"
font.bold: true
width: parent.width
//Layout.rightMargin: 10
}
}
Label {
text: "number"
color: "red"
Layout.fillWidth: true
}
SpinBox {
id: spinBox1
height: 30
stepSize: 1
editable: true
Layout.fillWidth: true
Layout.rightMargin: 10
}
}
}
}
}
Существует способ проверить тип компонента, но нам нужно передать его id
:
function isTextField(item) {
return item instanceof TextField
}
Есть ли изменения в использовании instaceof
с использованием другой ссылки, которая не является id
компонента?
Я думал о получении children
component
как это
var objects = configgrid.children