QAbstractSpinBox 的函数验证
Function validate of QAbstractSpinBox
我正在阅读 QT Docu
中的这个函数
它说
QValidator::State QAbstractSpinBox::validate(QString &input, int &pos) const [virtual]
This virtual function is called by the QAbstractSpinBox to determine whether input is valid. The pos parameter indicates the position in the string. Reimplemented in the various subclasses.
我有一个奇怪的问题,因为我不太理解文档。这里的input是一个字符串,我们判断input是否有效。那么,为什么我们需要字符串中的位置呢?我以为这里的pos是字符串的长度,但是调试的时候发现不是。那么这里的pos是什么意思呢?
更新:
感谢@mohabouje。在我的例子中,我使用了从 QAbstractSpinBox
继承的 class,我重写了这个方法并希望在更改后验证字符串。我如何更新此 pos
以进行验证?
QValidator::State MySpinBox::validate( QString &input, int &pos ) const
{
QString pureValue = stripped( input, &tmpPos, prefix(), suffix() ); //this is my function, i just want to remove also prefix and suffix
//I want to add group separator into the pureValue and validate it after that
//I want to add group separator here, not in the constructor with setGroupSeparatorShown(true);
//When i add group separator here, the separator appears simultaneously when we type
//When i set setGroupSeparatorShown(true) in the constructor, it appears after we finish editing and move to another thing (this element loses focus)
pureValue.insert(3, locale().groupSeparator());
input = pureValue;
// I think now 'pos' has changed, how could I update 'pos' to call the following function?
QValidator::State state = QDoubleSpinBox::validate( input, pos );
return state;
}
我对底层实现很好奇。我查看了 github 中的源代码。
QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
QValidator::State &state) const
{
if (cachedText == input && !input.isEmpty()) {
state = cachedState;
QSBDEBUG() << "cachedText was '" << cachedText << "' state was "
<< state << " and value was " << cachedValue;
return cachedValue;
}
const double max = maximum.toDouble();
const double min = minimum.toDouble();
QString copy = stripped(input, &pos);
QSBDEBUG() << "input" << input << "copy" << copy;
int len = copy.size();
...
}
这些参数在名为 stripped
的私有函数中使用。这是源代码:
QString QAbstractSpinBoxPrivate::stripped(const QString &t, int *pos) const
{
QString text = t;
if (specialValueText.size() == 0 || text != specialValueText) {
int from = 0;
int size = text.size();
bool changed = false;
if (prefix.size() && text.startsWith(prefix)) {
from += prefix.size();
size -= from;
changed = true;
}
if (suffix.size() && text.endsWith(suffix)) {
size -= suffix.size();
changed = true;
}
if (changed)
text = text.mid(from, size);
}
const int s = text.size();
text = text.trimmed();
if (pos)
(*pos) -= (s - text.size());
return text;
}
因此,如果我理解正确,给定一个字符串和 prefix/suffix 配置,该函数将获取该字符串并计算要验证的数据的实际大小,忽略前缀和后缀。
函数returns已经验证的数据可以被解析以计算数值。
pos的原始值,函数减去待校验文本大小与裁剪后文本大小的差值
我正在阅读 QT Docu
中的这个函数它说
QValidator::State QAbstractSpinBox::validate(QString &input, int &pos) const [virtual]
This virtual function is called by the QAbstractSpinBox to determine whether input is valid. The pos parameter indicates the position in the string. Reimplemented in the various subclasses.
我有一个奇怪的问题,因为我不太理解文档。这里的input是一个字符串,我们判断input是否有效。那么,为什么我们需要字符串中的位置呢?我以为这里的pos是字符串的长度,但是调试的时候发现不是。那么这里的pos是什么意思呢?
更新:
感谢@mohabouje。在我的例子中,我使用了从 QAbstractSpinBox
继承的 class,我重写了这个方法并希望在更改后验证字符串。我如何更新此 pos
以进行验证?
QValidator::State MySpinBox::validate( QString &input, int &pos ) const
{
QString pureValue = stripped( input, &tmpPos, prefix(), suffix() ); //this is my function, i just want to remove also prefix and suffix
//I want to add group separator into the pureValue and validate it after that
//I want to add group separator here, not in the constructor with setGroupSeparatorShown(true);
//When i add group separator here, the separator appears simultaneously when we type
//When i set setGroupSeparatorShown(true) in the constructor, it appears after we finish editing and move to another thing (this element loses focus)
pureValue.insert(3, locale().groupSeparator());
input = pureValue;
// I think now 'pos' has changed, how could I update 'pos' to call the following function?
QValidator::State state = QDoubleSpinBox::validate( input, pos );
return state;
}
我对底层实现很好奇。我查看了 github 中的源代码。
QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
QValidator::State &state) const
{
if (cachedText == input && !input.isEmpty()) {
state = cachedState;
QSBDEBUG() << "cachedText was '" << cachedText << "' state was "
<< state << " and value was " << cachedValue;
return cachedValue;
}
const double max = maximum.toDouble();
const double min = minimum.toDouble();
QString copy = stripped(input, &pos);
QSBDEBUG() << "input" << input << "copy" << copy;
int len = copy.size();
...
}
这些参数在名为 stripped
的私有函数中使用。这是源代码:
QString QAbstractSpinBoxPrivate::stripped(const QString &t, int *pos) const
{
QString text = t;
if (specialValueText.size() == 0 || text != specialValueText) {
int from = 0;
int size = text.size();
bool changed = false;
if (prefix.size() && text.startsWith(prefix)) {
from += prefix.size();
size -= from;
changed = true;
}
if (suffix.size() && text.endsWith(suffix)) {
size -= suffix.size();
changed = true;
}
if (changed)
text = text.mid(from, size);
}
const int s = text.size();
text = text.trimmed();
if (pos)
(*pos) -= (s - text.size());
return text;
}
因此,如果我理解正确,给定一个字符串和 prefix/suffix 配置,该函数将获取该字符串并计算要验证的数据的实际大小,忽略前缀和后缀。
函数returns已经验证的数据可以被解析以计算数值。
pos的原始值,函数减去待校验文本大小与裁剪后文本大小的差值