Results 1 to 16 of 16

Thread: QLineEdit behaviour on deleted input

  1. #1
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default QLineEdit behaviour on deleted input

    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:

    Qt Code:
    1. QRegExp LongLongRegExp("[LONG_MIN, LONG_MAX]");
    2.  
    3. TSSimTimeIncrSecondsEdit->setValidator (new QRegExpValidator(LongLongRegExp,NULL));
    To copy to clipboard, switch view to plain text mode 

    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,

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit behaviour on deleted input

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit behaviour on deleted input

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit behaviour on deleted input

    Quote Originally Posted by vulcan_raven View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit behaviour on deleted input

    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

    Qt Code:
    1. QRegExp LongLongRegExp("[LONG_MIN, -3]||[10,LONG_MAX]");
    To copy to clipboard, switch view to plain text mode 

    and set the widget validator with
    Qt Code:
    1. TSSimTimeIncrNanosecondsEdit->setValidator(new QRegExpValidator(LongLongRegExp,NULL));
    To copy to clipboard, switch view to plain text mode 

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

    Care to help?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit behaviour on deleted input

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit behaviour on deleted input

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit behaviour on deleted input

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit behaviour on deleted input

    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 :

    Qt Code:
    1. QNonZeroLongValidator::QNonZeroLongValidator(QObject * parent): QValidator(parent),
    2. m_NegativeValueValidator(LONG_MIN, -1, parent),
    3. m_PositiveValueValidator(1, LONG_MAX, parent)
    4. {
    5.  
    6. }
    7.  
    8. QValidator::State QNonZeroLongValidator::validate(QString &input, int &pos) const
    9. {
    10. if ((m_NegativeValueValidator.validate(input,pos) == Acceptable) || (m_PositiveValueValidator.validate(input,pos) == Acceptable))
    11. {
    12. return Acceptable;
    13. }
    14. else
    15. {
    16. if ((m_NegativeValueValidator.validate(input,pos) == Intermediate) || (m_PositiveValueValidator.validate(input,pos) == Intermediate))
    17. {
    18. return Intermediate;
    19. }
    20. else
    21. {
    22. return Invalid;
    23. }
    24. }
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit behaviour on deleted input

    Because after '0' you may type '2' and be inside the range accepted by the validator so '0' is a good intermediate value.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit behaviour on deleted input

    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 :-)

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit behaviour on deleted input

    What's wrong with 00002?

    For this particular range you may use this regular expression: "-?[1-9][0-9]*"
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit behaviour on deleted input

    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...

  14. #14
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit behaviour on deleted input

    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?

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit behaviour on deleted input

    Either QLineEdit or QSpinBox. But in both cases you will have to manipulate with the validator.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Dec 2006
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QLineEdit behaviour on deleted input

    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.

    Qt Code:
    1. leEditor = new QLineEdit(parent);
    2. leEditor->setText(<the text>);
    3. leEditor->setSelection(0, <the text length>);
    4. editor = leEditor;
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. QLineEdit and input mask
    By tpf80 in forum Qt Programming
    Replies: 7
    Last Post: 9th May 2014, 19:47
  2. QLineEdit behaviour
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2010, 14:37
  3. Strange behaviour QLineedit with edit mask
    By vcp in forum Qt Programming
    Replies: 4
    Last Post: 6th May 2010, 12:01
  4. How to use input method with QLineEdit in HP-UX (Qt4.5)
    By justalittle in forum Qt Programming
    Replies: 0
    Last Post: 11th March 2010, 08:22
  5. QLineEdit check Input
    By raphaelf in forum Newbie
    Replies: 1
    Last Post: 17th August 2006, 14:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.