Qt Quick:如何从 ComboBox 获取当前文本
Qt Quick: How to get the current text from ComboBox
我尝试从 ComboBox 的当前索引中获取文本。 model.get(scale).text
方法不起作用,它只获取第一个索引。
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.0
ApplicationWindow {
visible: true
width: 200
height: 200
title: qsTr("Hello World")
Column{
anchors.horizontalCenter: parent.horizontalCenter
id:optionsColumn
spacing: 5
anchors.fill : parent
anchors.topMargin : 15
anchors.bottomMargin : 15
anchors.leftMargin :15
anchors.rightMargin : 15
ComboBox {
id: scale
anchors.horizontalCenter: parent.horizontalCenter
editable: true
inputMethodHints: Qt.ImhDigitsOnly
currentIndex: 1
model: ListModel {
id: model
ListElement { text: "500" }
ListElement { text: "1000" }
ListElement { text: "2000" }
ListElement { text: "5000" }
ListElement { text: "10000" }
ListElement { text: "25000" }
}
onAccepted: {
id:maccepted
if (find(editText) === -1)
model.append({text: editText})
}
}
Button{
id:showtext
text: "Scale to text"
anchors.horizontalCenter: parent.horizontalCenter
onClicked: {
var a=parseFloat(model.get(scale).text)
scaletext.text=a
}
}
TextField{
id:scaletext
anchors.horizontalCenter: parent.horizontalCenter
readOnly: true
}
}
}
玩得开心。
onClicked: {
//var a=parseFloat(model.get(scale).text)
scaletext.text = scale.currentText;
}
我尝试从 ComboBox 的当前索引中获取文本。 model.get(scale).text
方法不起作用,它只获取第一个索引。
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.0
ApplicationWindow {
visible: true
width: 200
height: 200
title: qsTr("Hello World")
Column{
anchors.horizontalCenter: parent.horizontalCenter
id:optionsColumn
spacing: 5
anchors.fill : parent
anchors.topMargin : 15
anchors.bottomMargin : 15
anchors.leftMargin :15
anchors.rightMargin : 15
ComboBox {
id: scale
anchors.horizontalCenter: parent.horizontalCenter
editable: true
inputMethodHints: Qt.ImhDigitsOnly
currentIndex: 1
model: ListModel {
id: model
ListElement { text: "500" }
ListElement { text: "1000" }
ListElement { text: "2000" }
ListElement { text: "5000" }
ListElement { text: "10000" }
ListElement { text: "25000" }
}
onAccepted: {
id:maccepted
if (find(editText) === -1)
model.append({text: editText})
}
}
Button{
id:showtext
text: "Scale to text"
anchors.horizontalCenter: parent.horizontalCenter
onClicked: {
var a=parseFloat(model.get(scale).text)
scaletext.text=a
}
}
TextField{
id:scaletext
anchors.horizontalCenter: parent.horizontalCenter
readOnly: true
}
}
}
玩得开心。
onClicked: {
//var a=parseFloat(model.get(scale).text)
scaletext.text = scale.currentText;
}