Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么使用IntValidator';QT中的底部不工作?_Qt_Qml - Fatal编程技术网

为什么使用IntValidator';QT中的底部不工作?

为什么使用IntValidator';QT中的底部不工作?,qt,qml,Qt,Qml,现在我在TextInput中使用IntValidator,其底部值大于0,如下所示: validator:IntValidator{底部:20;顶部:100;} 但我仍然可以输入一个不需要的值,如10,并且在失去焦点后输入的文本将无法清除。 我不知道为什么?任何人都可以帮助我。每次键入字符时,IntValidator都会尝试验证您的输入,并返回3种可能的状态:无效、中间和可接受 想象一个场景,您将底部值设置为20,顶部值设置为1000。 现在您想输入100,但IntValidator会报告1、1

现在我在TextInput中使用IntValidator,其底部值大于0,如下所示:
validator:IntValidator{底部:20;顶部:100;}
但我仍然可以输入一个不需要的值,如10,并且在失去焦点后输入的文本将无法清除。
我不知道为什么?任何人都可以帮助我。

每次键入字符时,IntValidator都会尝试验证您的输入,并返回3种可能的状态:无效、中间和可接受

想象一个场景,您将底部值设置为20,顶部值设置为1000。 现在您想输入100,但IntValidator会报告1、10无效,因为这些值小于20,因此它不允许您输入任何值,这就是它返回中间值的原因

您可以看到Qt的源代码中发生了什么:

QValidator::State QIntValidator::validate(QString & input, int&) const
{
    QByteArray buff;
    if (!locale().d->m_data->validateChars(input, QLocaleData::IntegerMode, &buff, -1,
                                           locale().numberOptions())) {
        return Invalid;
    }
    if (buff.isEmpty())
        return Intermediate;
    const bool startsWithMinus(buff[0] == '-');
    if (b >= 0 && startsWithMinus)
        return Invalid;
    const bool startsWithPlus(buff[0] == '+');
    if (t < 0 && startsWithPlus)
        return Invalid;
    if (buff.size() == 1 && (startsWithPlus || startsWithMinus))
        return Intermediate;
    bool ok;
    qlonglong entered = QLocaleData::bytearrayToLongLong(buff.constData(), 10, &ok);
    if (!ok)
        return Invalid;
    if (entered >= b && entered <= t) {
        locale().toInt(input, &ok);
        return ok ? Acceptable : Intermediate;
    }
    if (entered >= 0) {
        // the -entered < b condition is necessary to allow people to type
        // the minus last (e.g. for right-to-left languages)
        // The buffLength > tLength condition validates values consisting
        // of a number of digits equal to or less than the max value as intermediate.
        int buffLength = buff.size();
        if (startsWithPlus)
            buffLength--;
        const int tLength = t != 0 ? static_cast<int>(std::log10(qAbs(t))) + 1 : 1;
        return (entered > t && -entered < b && buffLength > tLength) ? Invalid : Intermediate;
    } else {
        return (entered < b) ? Invalid : Intermediate;
    }
}
QValidator::State QIntValidator::validate(QString&input,int&)const
{
QByteArray buff;
如果(!locale().d->m_data->validateChars(输入,QLocaleData::IntegerMode,&buff,-1,
locale().numberOptions()){
返回无效;
}
if(buff.isEmpty())
返回中间体;
常量布尔开始于负(buff[0]='-');
如果(b>=0&&startswith减号)
返回无效;
常量布尔开始与加号(buff[0]='+');
if(t<0&&startsWithPlus)
返回无效;
if(buff.size()==1&&(startsWithPlus | | startsWithPlus))
返回中间体;
布尔ok;
输入的qlonglong=QLocaleData::bytearrayToLongLong(buff.constData(),10,&ok);
如果(!ok)
返回无效;
如果(输入>=b&&entered=0){
//输入的tLength条件验证包含以下内容的值:
//中间数等于或小于最大值的位数。
int buffLength=buff.size();
如果(启动WithPlus)
长度--;
常量长度=t!=0?静态(标准::log10(qAbs(t))+1:1;
返回值(输入>t&&-输入t长度)?无效:中间值;
}否则{
返回值(输入
如果要达到验证某个范围内的整数的目标,最好让用户键入除字母和特殊字符以外的任何字符(-+应被允许作为第一个字符)使用RegExpValidator并订阅editingFinished和activeFocusChanged信号,这样当用户按下enter键或离开TextInput字段时,您可以自己验证输入值,甚至将其更新到所需的范围内。

请提供