Reputation: 361
I am reading this function in QT Docu
It says
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.
I have a weird question because I dont really understand the document. The input here is a string, we determine whether the input is valid or not. So, why do we need the position in the string, for what? I thought the pos here is the length of the string but when I debugged, it is not true. So what does the pos here mean?
UPDATE:
Thanks to @mohabouje. In my case I use a class inherited from QAbstractSpinBox
, I override this method and want to validate the string after changed it. How could I update this pos
for validating?
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;
}
Upvotes: 1
Views: 429
Reputation: 4050
I was curious about the underlying implementation. I checked the source code in 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();
...
}
The parameters are used in a private function called stripped
. This is the source code:
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;
}
So, If I understand properly, given a string and prefix/suffix configuration, the function takes the string and computes the real size of the data to be validated, ignoring the prefix and suffix.
The function returns the data already validated that may be parsed to compute the numerical value.
The original value of pos, the function subtract the difference of the size of the text to be validated and the size of the text after performing the trimming operation.
Upvotes: 1