Results 1 to 5 of 5

Thread: QLineEdit: how to avoid characters insertion?

  1. #1
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QLineEdit: how to avoid characters insertion?

    Hi,
    I would like to control the input of a QlineEdit in order to avoid the insertion of some characters.
    I've written down the subsequent code that every time the user insert a new element in the QLineEdit, controls the input and erase the last character of the line if it is not valid!


    Qt Code:
    1. void ElaborationOffset::ElaborationOffset()
    2. {
    3. [...]
    4. par2 = new QLineEdit;
    5. connect(par2,SIGNAL(textChanged(QString)),this,SLOT(updateGraph(QString)));
    6. [...]
    7. }
    8.  
    9. void ElaborationOffset::updateGraph(QString dummy)
    10. {
    11. bool ok;
    12. int pos=0;
    13. QString par2Str = par2->text();
    14.  
    15. QRegExp expr("[-,+]{0,1}[0-9]+[.,,][0-9]+");
    16. QRegExpValidator v(expr, 0);
    17.  
    18. if(v.validate(par2Str,pos) == QValidator::Invalid)
    19. {
    20. par2->backspace();
    21. return;
    22. }
    23.  
    24. [...]
    25. }
    To copy to clipboard, switch view to plain text mode 

    It works... But the problem is that when I try to insert a non valid char, the updateGraph slot is called 2 times, because of the par2->backspace(); instruction that triggers another signal--->slot call !!!
    I think there is no mask that I can use to avoid this problem, because I cannot know the number format (it depends on the particular value)...! For example I can insert -0.0002, but also 12900!!!
    Any hints to solve this problem?

    Thank you all.
    Alex

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit: how to avoid characters insertion?

    just a little suggestion .. use editingFinished() signal instead of textChanged() if user only edits the lineEdit ...
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLineEdit: how to avoid characters insertion?

    Have a look at QLineEdit::setValidator.

  4. #4
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit: how to avoid characters insertion?

    Quote Originally Posted by wagmare View Post
    just a little suggestion .. use editingFinished() signal instead of textChanged() if user only edits the lineEdit ...
    Yes... could be a solution, but I'd like to avoid completely the insertion of letters (I mean A-Z, a-z).
    I want that the user is completely unable to insert the avoided characters!

  5. #5
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit: how to avoid characters insertion?

    Quote Originally Posted by boudie View Post
    Have a look at QLineEdit::setValidator.
    OK!! This is the solution. I tried before, but It didn't function because I wrote the subsequent code:

    par2 = new QLineEdit;
    QRegExp expr("[-,+]{0,1}[0-9]+[.,,][0-9]+");
    QRegExpValidator v(expr, 0);
    par2->setValidator(&v);
    instead of:


    par2 = new QLineEdit;
    QRegExp expr("[-,+]{0,1}[0-9]+[.,,][0-9]+");
    QRegExpValidator *v = new QRegExpValidator(expr, 0);
    par2->setValidator(v);
    Well... thank you for your hint...
    Another question: Could you please explain why the first solution doesn't work, while the second works?
    I have encountered this problem other times, but I've not clear the real cause of this problem!

    Thank you very much!

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

    for_hope (18th January 2010)

Similar Threads

  1. How to adjust spacing between characters in QLineEdit?
    By bzjbest in forum Qt Programming
    Replies: 2
    Last Post: 2nd January 2008, 07:44
  2. Insertion of unicode characters into database oracle through pro c
    By hemananda choudhuri in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2007, 10:42
  3. QTableWidget row swapping and insertion
    By fritz in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2006, 04:07
  4. double record insertion
    By tranfuga25s in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2006, 17:12
  5. Multiple insertion using QSqlTableModel
    By munna in forum Newbie
    Replies: 3
    Last Post: 9th May 2006, 20:20

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.