PDA

View Full Version : QComoBox and Validator



cyberduck
26th July 2012, 22:33
Hi,

if i set a Subclassed Validator to a QLineEdit it works perfect. But when i try it
on a QComboBox:


ui.Box->lineEdit()->setValidator( new Validator(0.00, 6, 2) );
the validation fails sometimes. The Validator works on a QComboBox too, if i type more numbers slowly. If the time between the numbers less than 1 second i can put 9 and 9 and 9 and..... to the ui.Box->lineEdit().

What is different between a QLineEdit and the ui.Box->lineEdit()? Can you help me to solve it?

Thanks in advance

amleto
27th July 2012, 00:58
Does the same problem happen with a qt validator? The problem is more likely to be your code than the Qt code...

While you're here, read my sig :)

cyberduck
27th July 2012, 10:16
A QDoubleValidator can't produce the same. The user can type any number into the QLineEit and the QValidator State is changing. In my case should the Validator accept only numbers from min to max. So i have wrote my own Validator:

class DoubleValidator : public QDoubleValidator
{
public:
DoubleValidator( double bottom, double top, int decimals, QObject* parent = 0)
: QDoubleValidator( bottom, top, decimals, parent){}

QValidator::State validate(QString & input, int &) const
{
const double b = bottom();
const double t = top();
const int d = decimals();

QRegExp empty(QString::fromLatin1("-?\\.?"));
if (input.contains(' '))
return Invalid;
if (b >= 0 && input.startsWith(QLatin1Char('-')))
return Invalid;
if (empty.exactMatch(input))
return Intermediate;

bool ok = false;
double entered = input.toDouble(&ok);
if (!ok) return Invalid;

int nume = input.count('e', Qt::CaseInsensitive);

int i;
if (input.contains(','))
i = input.indexOf(',');
else
i = input.indexOf('.');

if (i >= 0 && nume == 0) {
// has decimal point (but no e), now count digits after that
i++;
int j = i;
while(input[j].isDigit())
j++;
if (j - i > d)
return Invalid;
}

if( entered < b ){
return Intermediate;
}else if( entered > t ){
return Invalid;
}

return Acceptable;
}
};
Can you see what's wrong with my Validator? Why doesn't work with ui.Box->lineEdit()and on a QLineEdit it's all fine?

cyberduck
27th July 2012, 15:02
Finally i think the reason is not my validator. I have try it with a QRegExpValidator and the same problem comes up. Here is a short example:


QRegExp rx("^([0-5][,.][0-9]{0,2}|[0-6][,.][0]{0,2}|[0-6])$");
ui.Box->lineEdit()->setValidator( new QRegExpValidator(rx, ui.Box->lineEdit() ) );

Maybe it's important i use Qt 4.7.2

Added after 32 minutes:

I've found the reason for that problem. My QLineEdit contains only numbers and my QComboBox
alphanumeric characters. In this case works all fine:



ui.Box->addItem("0,50");
ui.Box->addItem("1,00");
ui.Box->addItem("1,50");


and that fails



ui.Box->addItem("0,50 Pieces");
ui.Box->addItem("1,00 Pieces");
ui.Box->addItem("1,50 Pieces");


How can i add items like this and valitate right?

amleto
27th July 2012, 15:09
change the regex

cyberduck
31st July 2012, 08:19
Sorry i don't understand what i have to change. My Regexp don't match "9999" but the ui.Box->lineEdit() accept it! Can you explain what i have to do, please?