Results 1 to 6 of 6

Thread: QComoBox and Validator

  1. #1
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question QComoBox and Validator

    Hi,

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

    Qt Code:
    1. ui.Box->lineEdit()->setValidator( new Validator(0.00, 6, 2) );
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComoBox and Validator

    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
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Re: QComoBox and Validator

    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:
    Qt Code:
    1. class DoubleValidator : public QDoubleValidator
    2. {
    3. public:
    4. DoubleValidator( 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. // has decimal point (but no e), now count digits after that
    35. i++;
    36. int j = i;
    37. while(input[j].isDigit())
    38. j++;
    39. if (j - i > d)
    40. return Invalid;
    41. }
    42.  
    43. if( entered < b ){
    44. return Intermediate;
    45. }else if( entered > t ){
    46. return Invalid;
    47. }
    48.  
    49. return Acceptable;
    50. }
    51. };
    To copy to clipboard, switch view to plain text mode 
    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?

  4. #4
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Re: QComoBox and Validator

    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:
    Qt Code:
    1. QRegExp rx("^([0-5][,.][0-9]{0,2}|[0-6][,.][0]{0,2}|[0-6])$");
    2. ui.Box->lineEdit()->setValidator( new QRegExpValidator(rx, ui.Box->lineEdit() ) );
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. ui.Box->addItem("0,50");
    2. ui.Box->addItem("1,00");
    3. ui.Box->addItem("1,50");
    To copy to clipboard, switch view to plain text mode 

    and that fails

    Qt Code:
    1. ui.Box->addItem("0,50 Pieces");
    2. ui.Box->addItem("1,00 Pieces");
    3. ui.Box->addItem("1,50 Pieces");
    To copy to clipboard, switch view to plain text mode 

    How can i add items like this and valitate right?
    Last edited by cyberduck; 27th July 2012 at 14:02.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComoBox and Validator

    change the regex
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Aug 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Re: QComoBox and Validator

    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?

Similar Threads

  1. QLineEdit validator
    By pobere in forum Newbie
    Replies: 2
    Last Post: 5th November 2011, 10:16
  2. How to create a validator
    By Cucus in forum Newbie
    Replies: 1
    Last Post: 4th August 2011, 07:47
  3. Validator
    By tinysoft in forum Newbie
    Replies: 5
    Last Post: 13th April 2011, 16:00
  4. Styling QComoBox using CSS Stylesheet
    By bjoernbg in forum Qt Programming
    Replies: 1
    Last Post: 6th October 2010, 23:12
  5. what's wrong with my validator?
    By TheKedge in forum Qt Programming
    Replies: 5
    Last Post: 26th January 2006, 21:41

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.