Results 1 to 3 of 3

Thread: Accept/Not accept symbols in QLineEdit

  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Accept/Not accept symbols in QLineEdit

    I have got a class MyLineEdit : QLineEdit

    All I want is it to accept symbols if acceptSymbol() returns true and not accept it if it returns false. Has anybody got any idea why my implementation not working?

    Qt Code:
    1. void MyLineEdit::keyPressEvent(QKeyEvent *key)
    2. {
    3.  
    4. if (key->key()==Qt::Key_Escape)
    5. emit escapePressed();
    6. else
    7. {
    8. if ((key->key() >=48 ) ||
    9. (key->key() <=57) ||
    10. (key->key() >=65 ) ||
    11. (key->key() <= 90) ||
    12. (key->key() >=97) ||
    13. (key->key() <=122))
    14. {
    15. QLineEdit::keyPressEvent(key);
    16. }
    17. else
    18. {
    19.  
    20. if (acceptSymbols())
    21. {
    22. QLineEdit::keyPressEvent(key);
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  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: Accept/Not accept symbols in QLineEdit

    Your complex if statement is always true.
    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
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Accept/Not accept symbols in QLineEdit

    Thanks nice spotted


    Added after 19 minutes:


    Found a better way

    Qt Code:
    1. #include "mylineedit.h"
    2. #include <QKeyEvent>
    3. #include <QRegExpValidator>
    4. MyLineEdit::MyLineEdit(QWidget *parent) :
    5. QLineEdit(parent)
    6. {
    7. setMinimumHeight(kDefaultHeight);
    8. setAcceptSymbols(true);
    9. }
    10.  
    11. void MyLineEdit::keyPressEvent(QKeyEvent *key)
    12. {
    13. if (key->key()==Qt::Key_Escape)
    14. emit escapePressed();
    15. else
    16.  
    17. QLineEdit::keyPressEvent(key);
    18. }
    19. void MyLineEdit::setAcceptSymbols(const bool val)
    20. {
    21. this->acceptSymbols_=val;
    22. if (!acceptSymbols())
    23. {
    24. QRegExp validNickname("^[a-zA-Z0-9_]*$"); //alpha-numeric + underscore
    25.  
    26. QValidator *validator=new QRegExpValidator(validNickname,this);
    27. setValidator(validator);
    28.  
    29. }
    30. }
    31. bool MyLineEdit::acceptSymbols(void) const
    32. {
    33. return this->acceptSymbols_;
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by naturalpsychic; 28th March 2012 at 16:30. Reason: if (!acceptSymbol()) not if (acceptSymbol())
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

Similar Threads

  1. How to set QLineEdit to accept only ASCII...
    By cydside in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2011, 08:55
  2. Masking QLineEdit to accept white space.
    By savaliya_ambani in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th February 2011, 11:45
  3. Replies: 3
    Last Post: 21st December 2010, 14:51
  4. accept() and reject() slots
    By poporacer in forum Newbie
    Replies: 3
    Last Post: 19th August 2010, 01:07
  5. Replies: 1
    Last Post: 31st December 2008, 16:55

Tags for this Thread

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.