185 lines
5.4 KiB
QML
185 lines
5.4 KiB
QML
import QtQuick
|
||
import QtQuick.Controls
|
||
import QtQuick.Layouts
|
||
|
||
Item {
|
||
id: control
|
||
|
||
property var value: Qt.point(0, 0)
|
||
property int decimals: 2
|
||
implicitHeight: 25
|
||
implicitWidth: 120 // 设置默认宽度
|
||
|
||
signal asValueChanged(value: var)
|
||
|
||
function setValue(newValue: var) {
|
||
if (!control.value ||
|
||
Math.abs(control.value.x - newValue.x) > 0.000001 ||
|
||
Math.abs(control.value.y - newValue.y) > 0.000001) {
|
||
value = newValue
|
||
asValueChanged(value)
|
||
}
|
||
}
|
||
|
||
RowLayout {
|
||
anchors.fill: parent
|
||
spacing: 4 // 减小间距
|
||
|
||
Label {
|
||
text: "X:"
|
||
Layout.alignment: Qt.AlignVCenter
|
||
font.pixelSize: 10
|
||
}
|
||
|
||
TextField {
|
||
id: xBox
|
||
Layout.preferredWidth: 60 // 减小宽度
|
||
Layout.preferredHeight: 22
|
||
Layout.alignment: Qt.AlignVCenter
|
||
text: control.value ? control.value.x.toFixed(control.decimals) : "0.00"
|
||
font.pixelSize: 10
|
||
leftPadding: 4
|
||
rightPadding: 4
|
||
selectByMouse: true
|
||
|
||
// 双击全选的处理
|
||
MouseArea {
|
||
id: xMouseArea
|
||
anchors.fill: parent
|
||
propagateComposedEvents: true
|
||
acceptedButtons: Qt.LeftButton
|
||
cursorShape: Qt.IBeamCursor
|
||
|
||
onDoubleClicked: function(mouse) {
|
||
parent.selectAll()
|
||
parent.forceActiveFocus()
|
||
mouse.accepted = true
|
||
}
|
||
|
||
onClicked: function(mouse) {
|
||
// 单击时设置焦点但不全选
|
||
parent.forceActiveFocus()
|
||
mouse.accepted = false
|
||
}
|
||
}
|
||
|
||
onActiveFocusChanged: {
|
||
if (activeFocus) {
|
||
// 如果不是双击触发的焦点变化,就全选文本
|
||
if (!xMouseArea.containsPress) {
|
||
selectAll()
|
||
}
|
||
}
|
||
}
|
||
|
||
validator: DoubleValidator {
|
||
bottom: -999999
|
||
top: 999999
|
||
decimals: 2
|
||
}
|
||
|
||
background: Rectangle {
|
||
color: xBox.enabled ? "white" : "#f0f0f0"
|
||
border.color: xBox.activeFocus ? "#2196F3" : "#cccccc"
|
||
border.width: 1
|
||
radius: 2
|
||
}
|
||
|
||
onEditingFinished: {
|
||
var xValue = parseFloat(text)
|
||
if (!isNaN(xValue) && control.value) {
|
||
control.setValue(Qt.point(xValue, control.value.y))
|
||
} else {
|
||
xBox.text = control.value ? control.value.x.toFixed(control.decimals) : "0.00"
|
||
}
|
||
}
|
||
}
|
||
|
||
Label {
|
||
text: "Y:"
|
||
Layout.alignment: Qt.AlignVCenter
|
||
font.pixelSize: 10
|
||
}
|
||
|
||
TextField {
|
||
id: yBox
|
||
Layout.preferredWidth: 60
|
||
Layout.preferredHeight: 22
|
||
Layout.alignment: Qt.AlignVCenter
|
||
text: control.value ? control.value.y.toFixed(control.decimals) : "0.00"
|
||
font.pixelSize: 10
|
||
leftPadding: 4
|
||
rightPadding: 4
|
||
selectByMouse: true
|
||
|
||
// 双击全选的处理
|
||
MouseArea {
|
||
id: yMouseArea
|
||
anchors.fill: parent
|
||
propagateComposedEvents: true
|
||
acceptedButtons: Qt.LeftButton
|
||
cursorShape: Qt.IBeamCursor
|
||
|
||
onDoubleClicked: function(mouse) {
|
||
parent.selectAll()
|
||
parent.forceActiveFocus()
|
||
mouse.accepted = true
|
||
}
|
||
|
||
onClicked: function(mouse) {
|
||
// 单击时设置焦点但不全选
|
||
parent.forceActiveFocus()
|
||
mouse.accepted = false
|
||
}
|
||
}
|
||
|
||
onActiveFocusChanged: {
|
||
if (activeFocus) {
|
||
// 如果不是双击触发的焦点变化,就全选文本
|
||
if (!yMouseArea.containsPress) {
|
||
selectAll()
|
||
}
|
||
}
|
||
}
|
||
|
||
validator: DoubleValidator {
|
||
bottom: -999999
|
||
top: 999999
|
||
decimals: 2
|
||
}
|
||
|
||
background: Rectangle {
|
||
color: yBox.enabled ? "white" : "#f0f0f0"
|
||
border.color: yBox.activeFocus ? "#2196F3" : "#cccccc"
|
||
border.width: 1
|
||
radius: 2
|
||
}
|
||
|
||
onEditingFinished: {
|
||
var yValue = parseFloat(text)
|
||
if (!isNaN(yValue) && control.value) {
|
||
control.setValue(Qt.point(control.value.x, yValue))
|
||
} else {
|
||
yBox.text = control.value ? control.value.y.toFixed(control.decimals) : "0.00"
|
||
}
|
||
}
|
||
}
|
||
|
||
Item {
|
||
Layout.fillWidth: true
|
||
}
|
||
}
|
||
|
||
// 当value从外部改变时,更新输入框显示
|
||
onValueChanged: {
|
||
if (value) {
|
||
if (!xBox.activeFocus) {
|
||
xBox.text = value.x.toFixed(decimals)
|
||
}
|
||
if (!yBox.activeFocus) {
|
||
yBox.text = value.y.toFixed(decimals)
|
||
}
|
||
}
|
||
}
|
||
}
|