Qt QAbstractSpinBox的功能验证
我正在读这个函数 上面说Qt QAbstractSpinBox的功能验证,qt,qdoublespinbox,Qt,Qdoublespinbox,我正在读这个函数 上面说 QValidator::State QAbstractSpinBox::validate(QString&input,int&pos)const[virtual] QAbstractSpinBox调用此虚拟函数以确定输入是否有效pos参数表示字符串中的位置。在不同的子类中重新实现 我有一个奇怪的问题,因为我真的不理解文件。这里的输入是一个字符串,我们确定输入是否有效。那么,为什么我们需要字符串中的位置,为什么?我以为这里的pos是字符串的长度,但当我调试时,它不是真的。
QValidator::State QAbstractSpinBox::validate(QString&input,int&pos)const[virtual]
QAbstractSpinBox调用此虚拟函数以确定输入是否有效pos参数表示字符串中的位置。在不同的子类中重新实现
我有一个奇怪的问题,因为我真的不理解文件。这里的输入是一个字符串,我们确定输入是否有效。那么,为什么我们需要字符串中的位置,为什么?我以为这里的pos是字符串的长度,但当我调试时,它不是真的。那么这里的pos是什么意思呢
更新:
感谢@mohaboje。在我的例子中,我使用从QAbstractSpinBox
继承的类,重写此方法,并希望在更改字符串后验证它。如何更新此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;
}
我对底层实现很好奇。我检查了源代码 因此,如果我理解正确,给定一个字符串和前缀/后缀配置,函数将获取字符串并计算要验证的数据的实际大小,而忽略前缀和后缀 该函数返回已验证的数据,这些数据可以通过解析来计算数值
pos的原始值,该函数减去要验证的文本大小与执行修剪操作后文本大小的差值。请非常小心,您所指的repo指向Qt 4.8,并且已6年未更新。OP最有可能使用的是Qt5.x,因此这很容易导致错误的结论,即使小部件的更改速度没有那么快。Woboq在这里更新了它:非常感谢,你能帮我更新一下吗?我不太确定你想做什么,但是你可以用你正在使用的(§“和“km”)覆盖默认前缀和后缀。将原始字符串strim以插入不同的后缀和前缀并再次strim是没有意义的。如果这样做,就不需要重写validate方法。@mohaboje:没错,当我再次调用validate时,我必须再次strimm。但是我想做的是,我想在字符串中添加
组分隔符
(点或逗号),然后验证它。因此,对于更新的字符串,我必须再次调用这个validate
。我现在不知道如何更新pos
。(请看我的更新)
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();
...
}
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;
}