Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: QLineEdit set min max range?

  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question QLineEdit set min max range?

    Hi,

    how can i set a Validator to a QLineEdit that accept only values between 1.00 and 16.00 ?

    Thanks in advance,

    Whitefurrows

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLineEdit set min max range?

    QSpinBox (or QDoubleSpinBox) would have the required behaviour, out of the box...
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    whitefurrows (11th June 2006)

  4. #3
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    I need a clear input field like a QLineEdit. I can't use a QSpinBox or QDoubleSpinBox because have a button on the right side.

    One more tip ?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit set min max range?

    Then use QDoubleValidator or QRegExpValidator (regexp should be something like "(?:1[0-5]|[1-9])(?:[.,]\\d\\d?)?|16(?:[.,]00?)?").

  6. The following user says thank you to jacek for this useful post:

    whitefurrows (11th June 2006)

  7. #5
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    Hi,

    that's great your regexp work fine. I read the doc for QRegExp but i dont know what you do. Can you describe how your regexp work and i understand that. The problem is i have many other ranges to set e.g. 0-50, 15-32, 65-105 and i can't change your regexp.

    How can i set a QDoubleValidator its's easey, but my code don't work. I can set values out of range. Why?
    Qt Code:
    1. lineEdit->setValidator(new QDoubleValidator( 1.00, 16.00, 2, lineEdit));
    To copy to clipboard, switch view to plain text mode 

    Greetings,

    Whitefurrows

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit set min max range?

    Quote Originally Posted by whitefurrows
    Can you describe how your regexp work and i understand that. The problem is i have many other ranges to set e.g. 0-50, 15-32, 65-105 and i can't change your regexp.
    In such case, IMO, it would be better if you create your own validator class, because regexps aren't very readable.

    Anyway, suppose you want a regexp for 65-105 range. Valid numbers are:
    65, 66, 67, 68, 69, 70, ..., 79, 80, ..., 89, 90, ..., 99, 100, 101, 102, 103, 104, 105

    There are three possibilities:
    • 6 and one of {5, 6, 7, 8, 9} -> 6[5-9]
    • one of {7, 8, 9} and a digit -> [7-9]\\d
    • 10 and one of {0, 1, 2, 3, 4, 5} -> 10[0-5]

    and you just have to join them: 6[5-9]|[7-9]\\d|10[0-5]

    Quote Originally Posted by whitefurrows
    How can i set a QDoubleValidator its's easey, but my code don't work. I can set values out of range. Why?
    Because you can enter 1500.00e-2, which is a valid number.
    Last edited by jacek; 21st May 2006 at 17:28. Reason: fixed a typo

  9. The following user says thank you to jacek for this useful post:

    whitefurrows (11th June 2006)

  10. #7
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    OK i have understand how work the regexp, thank you.

    I think really it's better to write my own validator class.

    Qt Code:
    1. class myDoubleValidator : public QDoubleValidator
    2. {
    3. public:
    4. myDoubleValidator( double bottom, double top, int decimals, QObject* parent = 0)
    5. : QDoubleValidator( bottom, top, decimals, parent){}
    6.  
    7. QValidator::State validate ( QString & input, int &pos ) const
    8. {
    9. if ( input.isEmpty() || input == "." )
    10. return Intermediate;
    11. //I'm not sure what can i do here
    12. return Invalid;
    13. }
    14.  
    15. return Acceptable;
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 

    Can you help me please?

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit set min max range?

    Take a look at QDoubleValidator::validate() implementation (it's in %QTDIR%/src/gui/widgets/qvalidator.cpp). All you have to do is to remove the part that handles the exponent.

  12. The following user says thank you to jacek for this useful post:

    whitefurrows (11th June 2006)

  13. #9
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    I don't want change orginal files. I try this:

    Qt Code:
    1. class MyDoubleValidator : public QDoubleValidator
    2. {
    3. public:
    4. MyDoubleValidator( double bottom, double top, int decimals, QObject* parent = 0)
    5. : QDoubleValidator( bottom, top, decimals, parent)
    6. {}
    7.  
    8. QValidator::State QDoubleValidator::validate(QString & input, int &) const
    9. {
    10. QRegExp empty(QString::fromLatin1("-?\\.?"));
    11. if (input.contains(' '))
    12. return Invalid;
    13. if (b >= 0 && input.startsWith(QLatin1Char('-')))
    14. return Invalid;
    15. if (empty.exactMatch(input))
    16. return Intermediate;
    17. bool ok = true;
    18. double entered = input.toDouble(&ok);
    19. int nume = input.count('e', Qt::CaseInsensitive);
    20. if (!ok) {
    21. // explicit exponent regexp
    22. QRegExp expexpexp(QString::fromLatin1("[Ee][+-]?(\\d*)$"));
    23. int eePos = expexpexp.indexIn(input);
    24. if (eePos > 0 && nume == 1) {
    25. QString mantissa = input.left(eePos);
    26. entered = mantissa.toDouble(&ok);
    27. if (!ok)
    28. return Invalid;
    29. if (expexpexp.cap(1).isEmpty())
    30. return Intermediate;
    31. } else if (eePos == 0) {
    32. return Intermediate;
    33. } else {
    34. return Invalid;
    35. }
    36. }
    37.  
    38. int i = input.indexOf('.');
    39. if (i >= 0 && nume == 0) {
    40. // has decimal point (but no E), now count digits after that
    41. i++;
    42. int j = i;
    43. while(input[j].isDigit())
    44. j++;
    45. if (j - i > d)
    46. return Intermediate;
    47. }
    48.  
    49. if (entered < b || entered > t)
    50. return Intermediate;
    51. return Acceptable;
    52. }
    53. };
    To copy to clipboard, switch view to plain text mode 

    but i can't compile!
    Last edited by whitefurrows; 21st May 2006 at 17:18.

  14. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit set min max range?

    Quote Originally Posted by whitefurrows
    I try this:
    You're on the right track.

    It should be:
    Qt Code:
    1. class MyDoubleValidator : public QDoubleValidator
    2. {
    3. public:
    4. MyDoubleValidator( double bottom, double top, int decimals, QObject* parent = 0)
    5. : QDoubleValidator( bottom, top, decimals, parent)
    6. {}
    7.  
    8. QValidator::State validate(QString & input, int &) const
    9. {
    10. // we don't have the access to private members of QDoubleValidator,
    11. // so we must simulate them:
    12. const double b = bottom();
    13. const double t = top();
    14. const int d = decimals();
    15.  
    16. QRegExp empty(QString::fromLatin1("-?\\.?"));
    17. if (input.contains(' '))
    18. return Invalid;
    19. ...
    20. }
    21. };
    To copy to clipboard, switch view to plain text mode 

  15. The following user says thank you to jacek for this useful post:

    whitefurrows (11th June 2006)

  16. #11
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: QLineEdit set min max range?

    Hi,

    that's work fine. Thank you for your great help.

    Greetings,

    Whitefurrows

  17. #12
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    Hi,

    sorry i have trouble with my own QDoubleValidator, i set a Range 5-25 but i can't type 15 in to the QLineEdit:
    myLineEdit->setValidator(new MyDoubleValidator( 5, 25, 2, myLineEdit));

    Can anybody help me?

    Greetings,

    Whitefurrows

    Qt Code:
    1. class MyDoubleValidator : public QDoubleValidator
    2. {
    3. public:
    4. MyDoubleValidator( double bottom, double top, int decimals, QObject* parent = 0)
    5. : QDoubleValidator( bottom, top, decimals, parent){}
    6.  
    7. QValidator::State validate(QString & input, int &) const
    8. {
    9. const double b = bottom();
    10. const double t = top();
    11. const int d = decimals();
    12.  
    13. QRegExp empty(QString::fromLatin1("-?\\.?"));
    14. if (input.contains(' '))
    15. return Invalid;
    16. if (b >= 0 && input.startsWith(QLatin1Char('-')))
    17. return Invalid;
    18. if (empty.exactMatch(input))
    19. return Intermediate;
    20.  
    21. bool ok = false;
    22. double entered = input.toDouble(&ok);
    23. if (!ok) return Invalid;
    24.  
    25. int nume = input.count('e', Qt::CaseInsensitive);
    26.  
    27. int i;
    28. if (input.contains(','))
    29. i = input.indexOf(',');
    30. else
    31. i = input.indexOf('.');
    32.  
    33. if (i >= 0 && nume == 0) {
    34. i++;
    35. int j = i;
    36. while(input[j].isDigit())
    37. j++;
    38. if (j - i > d)
    39. return Invalid;
    40. }
    41.  
    42. if (entered < b || entered > t)
    43. return Invalid;
    44.  
    45. return Acceptable;
    46. }
    47. };
    To copy to clipboard, switch view to plain text mode 

  18. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit set min max range?

    The problem is here:
    Qt Code:
    1. if (entered < b || entered > t)
    2. return Invalid;
    To copy to clipboard, switch view to plain text mode 
    If the allowed range is 5--25, then "1" should be an intermediate value, not invalid one.
    It should be probably:
    Qt Code:
    1. if( entered < b ) {
    2. return Intermediate;
    3. }
    4. else if( entered > t ) {
    5. return Invalid;
    6. }
    To copy to clipboard, switch view to plain text mode 

  19. The following user says thank you to jacek for this useful post:

    whitefurrows (11th June 2006)

  20. #14
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    Hi,

    sorry i don't have get a notification. That's right, but the user can put wrong values to the QLineEdit.

    What can i do?

    Greetings,

    Whitefurrows

  21. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit set min max range?

    Quote Originally Posted by whitefurrows
    That's right, but the user can put wrong values to the QLineEdit.
    The problem is that line edit can loose focus when it contains an intermediate value. IMO you should ask the Trolls whether this is a correct behavior.

  22. The following user says thank you to jacek for this useful post:

    whitefurrows (11th June 2006)

  23. #16
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    Hi,

    thank's to you. You know a way how can i set focus to a QLineEdit as long as contains an intermediate value?

    Greetings

    Whitefurrows

  24. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLineEdit set min max range?

    There are many ways to do that. You could override line edit's focusOutEvent(), use an event filter, or install an event filter even on the application. In all cases, you'll just have to ask the validator for it's state and then set the focus back on the line edit when appropriate.
    J-P Nurmi

  25. The following user says thank you to jpn for this useful post:

    whitefurrows (11th June 2006)

  26. #18
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    Hi,

    can you help me to do that, please?

    I try that:

    Qt Code:
    1. protected:
    2. QLineEdit focusOutEvent(QFocusEvent* event) const;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QLineEdit MyWidget::focusOutEvent(QFocusEvent* event) const{
    2. const QObject* ob=QObject::sender();
    3. int i =0;
    4. while (ob!=lineEditList[i] && i<lineEditList.size())
    5. i++;
    6.  
    7. if(LineEdit Validator state == intermediate) //How can i do that?
    8. leList[i]->setEditFocus(true);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance,

    Whitefurrows

  27. #19
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLineEdit set min max range?

    The focus out event could look for example something like this:
    Qt Code:
    1. void MyLineEdit::focusOutEvent(QFocusEvent* event)
    2. {
    3. // check validator state
    4. int pos = 0;
    5. QValidator::State state = validator()->validate(text(), pos);
    6.  
    7. if (state == QValidator::Intermediate)
    8. {
    9. // keep focus if intermediate
    10. setFocus();
    11. }
    12. else
    13. {
    14. // otherwise call base class implementation and let it
    15. // handle focus out actions (so cursor stops blinking etc.)
    16. QLineEdit::focusOutEvent(event);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    If you have difficulties in subclassing QLineEdit, I suggest you to switch back to your favourite C++ book and learn the secrects of inheritance..
    J-P Nurmi

  28. The following user says thank you to jpn for this useful post:

    whitefurrows (11th June 2006)

  29. #20
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QLineEdit set min max range?

    Hi,

    the problem is i can't subclassing QLineEdit. I use the designer and the designer use the QLineEdit and not MyLineEdit.

    Sorry my stupid question i'm a beginner and learn. Please help me one more time.

    Greetings,

    Whitefurrows

Similar Threads

  1. Replies: 10
    Last Post: 12th February 2009, 08:23
  2. QLineEdit
    By rick_st3 in forum Newbie
    Replies: 1
    Last Post: 14th June 2008, 10:05
  3. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 16:13
  4. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 02:25
  5. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 13:48

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.