如何在带有DoubleValidator的QML SpinBox中防止超出范围的中间文本值?

2024-09-30 22:27:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个可编辑的浮点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
    }
}

Tags: fromposimportreturnvaluepropertyrealpyqt5
1条回答
网友
1楼 · 发布于 2024-09-30 22:27:38

我找到了问题的答案。我找到了SpinBox QML元素的源代码,并复制了TextInputcontentItem的代码。然后,我在onTextEdited中编写了一个快速函数,将值与SpinBox中的tofrom进行比较。它不是最可重用的解决方案,但我只需要它。另外,确保导入QtQuick.Control.impl以获取默认值。你知道吗

import QtQuick.Controls.impl 2.2

SpinBox {
    id: spinbox

    ...

    contentItem: TextInput {
        id: spinboxTextInput

        property string oldText: spinboxTextInput.text

        z: 2
        text: spinbox.textFromValue(spinbox.value, spinbox.locale)
        opacity: spinbox.enabled ? 1 : 0.3

        font: spinbox.font
        color: Default.textColor
        selectionColor: Default.focusColor
        selectedTextColor: Default.textLightColor
        horizontalAlignment: Qt.AlignHCenter
        verticalAlignment: Qt.AlignVCenter

        readOnly: !spinbox.editable
        validator: spinbox.validator
        inputMethodHints: spinbox.inputMethodHints

        //Check the value of the new text, and revert back if out of range
        onTextEdited: {
            var val = spinbox.valueFromText(spinboxTextInput.text, spinbox.locale)
            if (val < spinbox.from || val > spinbox.to) {
                spinboxTextInput.text = spinboxTextInput.oldText
            }
            else {
                spinboxTextInput.oldText = spinboxTextInput.text
            }
        }

        Rectangle {
            x: -6 - (spinbox.down.indicator ? 1 : 0)
            y: -6
            width: spinbox.width - (spinbox.up.indicator ? spinbox.up.indicator.width - 1 : 0) - (spinbox.down.indicator ? spinbox.down.indicator.width - 1 : 0)
            height: spinbox.height
            visible: spinbox.activeFocus
            color: "transparent"
            border.color: Default.focusColor
            border.width: 2
        }
    }
}

相关问题 更多 >