我正在尝试创建一个可编辑的浮点SpinBox QML元素。除了可以键入数字,使SpinBox中的文本显示一个无效值(例如,最大值为100时为105)之外,所有这些都可以工作。我试着用手指抓住按键按键。按下,但这似乎不可能。我也尝试过使用像onTextChanged这样的信号,但对于SpinBox来说似乎不存在这种情况。最后,我尝试将QValidator子类化,并将其用作spinbox的验证器,但出现了“cannotassignobjecttoproperty”错误。我假设这是因为我制作的自定义验证器不是验证器QML类型。你知道吗
自旋框-测试.py
import sys
from PyQt5 import QtGui
from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType
from PyQt5.QtWidgets import QApplication
class MyDoubleValidator(QtGui.QValidator):
def __init__(self, parent=None):
QtGui.QValidator.__init__(self, parent)
print("Validator created")
def validate(self, inputStr, pos):
print("validating")
if len(inputStr) > 2:
return (QtGui.QValidator.Invalid, pos)
elif len(inputStr) == 0:
return (Qt.QValidator.Intermediate, pos)
else:
return (Qt.Qvalidator.Acceptable, pos)
app = QApplication(sys.argv)
qmlRegisterType(MyDoubleValidator, 'MyValidators', 1, 0, 'MyDoubleValidator')
engine = QQmlApplicationEngine()
engine.load("spinbox-test.qml")
自旋框-测试.qml
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import MyValidators 1.0
ApplicationWindow {
visible: true
title: qsTr("Spinbox Test")
width: 400
height: 350
color: "whitesmoke"
Item {
id: doubleSpinbox
property int decimals: 2
property real realValue: 1.1
property real realFrom: 0.0
property real realTo: 100.0
property real realStepSize: 1.0
anchors.centerIn: parent
SpinBox {
id: spinbox
property real factor: Math.pow(10, doubleSpinbox.decimals)
stepSize: doubleSpinbox.realStepSize * spinbox.factor
value: doubleSpinbox.realValue * spinbox.factor
to: doubleSpinbox.realTo * spinbox.factor
from: doubleSpinbox.realFrom * spinbox.factor
editable: true
onValueChanged: label.text = spinbox.value / spinbox.factor
validator: MyDoubleValidator { }
textFromValue: function(value, locale) {
return parseFloat(spinbox.value*1.0/spinbox.factor).toFixed(doubleSpinbox.decimals);
}
}
}
Label {
id: label
text: doubleSpinbox.realValue
}
}
我找到了问题的答案。我找到了SpinBox QML元素的源代码,并复制了TextInput
contentItem
的代码。然后,我在onTextEdited
中编写了一个快速函数,将值与SpinBox中的to
和from
进行比较。它不是最可重用的解决方案,但我只需要它。另外,确保导入QtQuick.Control.impl
以获取默认值。你知道吗相关问题 更多 >
编程相关推荐