PDA

View Full Version : QLineEdit behaviour on deleted input



vulcan_raven
20th September 2010, 08:17
Hi all,

I am working on a GUI (built by using QDesigner) in which I have several QLineEdit widgets.

Inputting in those widgets works fine, however there is a behaviour that I do not quite understand and I might be missing something:

When the widget already has something in it and the user clicks inside the box to position the cursor at the end of what is there (not selecting everything but just positioning the cursor), "backspacing his way" until all the text is erased seems to "lock" the widget from allowing any future inputs! And I don't understand why?

Here is the code I have for the Validator of the widget:



QRegExp LongLongRegExp("[LONG_MIN, LONG_MAX]");

TSSimTimeIncrSecondsEdit->setValidator (new QRegExpValidator(LongLongRegExp,NULL));



I have not changed any other properties for the widget in my code.

Can someone explain what is going on and how to solve this?

Thank you,

wysota
20th September 2010, 08:58
What is the purpose of this validator? The only thing it will accept is a "[LONG_MIN, LONG_MAX]" string which I doubt is what you want. If you wanted to be able to input integers, use QIntValidator.

vulcan_raven
20th September 2010, 09:21
Hi Wysota... The only reason why I posted the code about the validator is to show that I didn't change any other parameter of the widget but this one... But if you must know, I used a regular expression to describe what I want my validator to do because I am testing various configurations that are variations of the IntValidator, DoubleValidator, etc. That is not the issue per say...

The problem occurs when I manually erase everything in the widget, I then cannot input any text or numbers in the QLineEdit.

wysota
20th September 2010, 09:25
That is not the issue per say...
In my opinion this is exactly the issue.


The problem occurs when I manually erase everything in the widget, I then cannot input any text or numbers in the QLineEdit.
Because probably your validator is incorrect and doesn't let you input something it considers invalid.

vulcan_raven
21st September 2010, 02:18
Hey Wysota...

I actually tried just commenting out my setValidator() code for my widget and I indeed do not get the strange behaviour I was talking about...

Guess you were right in saying that it was indeed the problem ;-)...

Now solving it is another matter. So in using that regular expression, my intent was to only allow a certain range of values to be valid (as you may have guessed).

It's not only about having Ints, Double, or Long values in there. It just so happens that the example I gave covered the whole range of Long values.

In reading the Qt documentation I thought one of the ways I could do what I wanted was to use the regular expressions. So for instance if I wanted all long values between in the range [LONG_MIN, -3] and [10, LONG_MAX] all I had to do was create



QRegExp LongLongRegExp("[LONG_MIN, -3]||[10,LONG_MAX]");


and set the widget validator with


TSSimTimeIncrNanosecondsEdit->setValidator(new QRegExpValidator(LongLongRegExp,NULL));


But then I guess I was wrong or something is missing...

Care to help?

wysota
21st September 2010, 07:48
LONG_MIN and "LONG_MIN" are two different things. QRegExp won't understand that by saying "LONG_MIN" you mean LONG_MIN. "LONG_MIN" is a string evaluated by QRegExp at runtime and LONG_MIN is a C define expanded by the C preprocessor during precompilation phase.

Use QIntValidator, an advanced QRegExpValidator or implement your own validator based on integers. Just be aware you won't be able to have a validator accepting ranges say... 3-5 and 45-67 at the same time - you will have to accept at least 6 too. I hope you get the picture.

vulcan_raven
21st September 2010, 08:08
Sorry wysota, I don't understand what you mean by



Just be aware you won't be able to have a validator accepting ranges say... 3-5 and 45-67 at the same time - you will have to accept at least 6 too.


Why can't I derive from QValidator and have my derived validate() method call the validate() of two different QIntValidators, one with the 3-5 range and the other with 45-67. If either of these methods return "Acceptable" state then my validator returns "Acceptable" as well. No?

Things are a bit more blurry when it comes to the "Intermediate" states, but it should be possible.

wysota
21st September 2010, 08:28
If you want the user to be able to input 3-5 or 45-67 then you will have to accept '6' too, otherwise one will not be able to enter 67. The validator will correctly return the value Intermediate but if you focus out of the widget at this moment, the value will stay in the line edit as a proper intermediate result. It doesn't matter how many validators you use, what matters is that one of them accepts the value out of range as the intermediate value (even though neither of the validators accepts this value explicitly).

vulcan_raven
21st September 2010, 08:43
Right... I get what you mean now...

I've tried deriving from QValidator as I described. My goal is to have a validator that accepts values in [LONG_MIN, -1] and [1, LONG_MAX] .... However I'm still capable of inputting "0" in the QLineEdit and I'm wondering why....

Here's a code snippet :



QNonZeroLongValidator::QNonZeroLongValidator(QObje ct * parent): QValidator(parent),
m_NegativeValueValidator(LONG_MIN, -1, parent),
m_PositiveValueValidator(1, LONG_MAX, parent)
{

}

QValidator::State QNonZeroLongValidator::validate(QString &input, int &pos) const
{
if ((m_NegativeValueValidator.validate(input,pos) == Acceptable) || (m_PositiveValueValidator.validate(input,pos) == Acceptable))
{
return Acceptable;
}
else
{
if ((m_NegativeValueValidator.validate(input,pos) == Intermediate) || (m_PositiveValueValidator.validate(input,pos) == Intermediate))
{
return Intermediate;
}
else
{
return Invalid;
}
}

}


Now why does the validate() return "Intermediate" when I input "0" ?

wysota
21st September 2010, 08:49
Because after '0' you may type '2' and be inside the range accepted by the validator so '0' is a good intermediate value.

vulcan_raven
21st September 2010, 08:56
Ah... Scratching my head here... I'm beginning to wonder if I don't need some type of input mask in there as well.

My intention is for a user to input a long value that is not zero, but without the possibility of having "000002" value possible for instance... Does that make sense?

A few examples of what I'd like:

123 => Valid/Intermediate
-3765 => Valid/Intermediate
00004 => Invalid
-00004 =>Invalid


Am I right in thinking I have to use an InputMask to achieve this?

Thanks for your help again :-)

wysota
21st September 2010, 09:34
What's wrong with 00002?

For this particular range you may use this regular expression: "-?[1-9][0-9]*"

vulcan_raven
21st September 2010, 09:44
;) Haha... Nothing is wrong per say with "00002" but in the current context of my GUI this would just seem weird... That's why I want to prevent the user from entering that kind of value.

The regular expression you suggest should be able to do the trick... I'll test it tomorrow and let you know...

vulcan_raven
22nd September 2010, 02:10
Hi,

I just realized that there is a potential issue with the regular expression you've given me...

It actually doesn't limit the value to LONG_MIN or LONG_MAX. I'm starting to believe that what I'm trying to achieve is more complicated than what I expected it to be.

Maybe I didn't choose the right solution for this by using QLineEdit widgets in my GUI.

What would have been an easy way to just have a widget by which the user inputs a number that has to be bound by values? :confused:

wysota
22nd September 2010, 10:16
Either QLineEdit or QSpinBox. But in both cases you will have to manipulate with the validator.

dugs
1st December 2010, 22:18
I am seeing this issue and I am using the default validator that is provided with QLineEdit.

I'm using the model view framework and in my delegate all I am doing is the following.



leEditor = new QLineEdit(parent);
leEditor->setText(<the text>);
leEditor->setSelection(0, <the text length>);
editor = leEditor;


I am having a similar issue if I create a QComboBox in the delegate and it has an empty string option. It will not let me select the empty string.

I'm using Qt 4.6.3