Results 1 to 6 of 6

Thread: set the validation for LineEdit

  1. #1
    Join Date
    Aug 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default set the validation for LineEdit

    I used keyboard design in my program.I want to set the values 18.5 to 39.0 only for keyboard's LineEdit.how to set the validation for this LineEdit?

  2. #2
    Join Date
    Sep 2011
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: set the validation for LineEdit

    Quote Originally Posted by sangee View Post
    I used keyboard design in my program.I want to set the values 18.5 to 39.0 only for keyboard's LineEdit.how to set the validation for this LineEdit?
    Here is a small Example: http://www.java2s.com/Code/Cpp/Qt/QD...dQLineEdit.htm

    Use QdoubleValidator.

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: set the validation for LineEdit

    Or this and this.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Aug 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: set the validation for LineEdit

    I was already saw this program.but doesn't work it.QDoubleValidator consider for decimal point only not ranges(18.5 to 39.0).how i set the validation for this LineEdit.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: set the validation for LineEdit

    QDoubleValidator checks the ranges. As documented, the line edit will not emit QLineEdit::editingFinished() or returnPressed() for invalid (in this case out-of-range) values. At any time QLineEdit::hasAcceptableInput() will return the current acceptability of the value. The validator does not stop you moving focus from the line edit with an intermediate value.

    Since you clearly will not believe us, run this program and try 18.4, 18.6, 39.0 and 40 followed by Tab or Return in the top edit box and watch the output.
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Widget: public QWidget
    5. {
    6. Q_OBJECT
    7. QLineEdit *line1;
    8. public:
    9. Widget(): QWidget() {
    10. line1 = new QLineEdit(this);
    11. QDoubleValidator *v = new QDoubleValidator(18.5, 39.0, 1, line1);
    12. v->setNotation(QDoubleValidator::StandardNotation);
    13. line1->setValidator(v);
    14.  
    15. QDoubleSpinBox *spin = new QDoubleSpinBox(this);
    16. spin->setDecimals(1);
    17. spin->setMinimum(18.0);
    18. spin->setMaximum(39.0);
    19.  
    20.  
    21. QVBoxLayout *layout = new QVBoxLayout(this);
    22. layout->addWidget(line1);
    23. layout->addWidget(spin);
    24. setLayout(layout);
    25.  
    26. connect(line1, SIGNAL(textChanged(QString)), SLOT(slotTextChanged(QString)));
    27. connect(line1, SIGNAL(returnPressed()), SLOT(checkIsValid()));
    28. connect(line1, SIGNAL(editingFinished()), SLOT(checkIsValid()));
    29. }
    30.  
    31. public slots:
    32. void slotTextChanged(const QString &text) {
    33. qDebug() << "Text changed to" << text << "and value is valid ==" << line1->hasAcceptableInput();
    34. }
    35. void checkIsValid() {
    36. qDebug() << "Edit finished and value is valid ==" << line1->hasAcceptableInput();
    37. }
    38. };
    39.  
    40. int main(int argc, char **argv)
    41. {
    42. QApplication app(argc, argv);
    43. Widget w;
    44. w.show();
    45. return app.exec();
    46. }
    47. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    What you really want is a QDoubleSpinBox which is more restrictive, just as easily edited, and always has a valid value.

  6. #6
    Join Date
    Aug 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: set the validation for LineEdit

    Thank u very much.

Similar Threads

  1. set the validation for checkboxes
    By sangee in forum Qt Programming
    Replies: 10
    Last Post: 24th August 2012, 09:45
  2. Validation Issue
    By StarRocks in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2012, 10:58
  3. XML Validation
    By NoRulez in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2010, 13:07
  4. Validation
    By Tavit in forum Qt Programming
    Replies: 2
    Last Post: 21st March 2009, 07:03
  5. Unique ID for a PC to use for Key Validation
    By nbkhwjm in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2008, 23:13

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
  •  
Qt is a trademark of The Qt Company.