【发布时间】:2019-04-27 00:19:19
【问题描述】:
我想做的是一个通用函数,它可以获取所有输入字段的信息(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
}
使用不是组件的id 的其他引用来使用instaceof 是否有变化?
我想像这样获得component 的children
var objects = configgrid.children
【问题讨论】:
-
你不能“传递 id”,id 不是一个属性,一般来说它是一个指向对象的指针。所以通过传递 id 你传递对对象的引用。但我仍然不确定 instaceof 是否可以返回除
Item或QObject之外的其他内容。应该检查一下。