Results 1 to 14 of 14

Thread: How to make LineEdit show all text in uppercase

  1. #1
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to make LineEdit show all text in uppercase

    Hi all!

    I'm making a gui app which contains some lineedits to let the user introduce some information. For that I set some QRegExp and QValidator and it works fine except for 2 cases that I hope that you can help me

    Case 1: a Lineedit that has to accept 3 letters which are ABC and max 5 numbers. For example... valid inputs: ABC12345 / ABC00000 / ABC1234 / ABC89865.... invalid inputs: ABC 1234 / AAA12345 / AB / ABC / A / ...
    I have this code but it acepts too A / AA / ... and I want to force the user to write ABC and at least 2 numbers

    Qt Code:
    1. QRegExp rx3("[A][B][C]\\d{5}");
    2. QValidator *validator3 = new QRegExpValidator(rx3, this);
    3. lineedit_case1->setValidator(validator3);
    To copy to clipboard, switch view to plain text mode 

    Another thing is that I want it to acept the user to put abc12345 BUT the line edit shows ABC12345. I mean: If the user write letters with caps lock or not, the line edits should always shows the text in uppercase. And with my code it only accepts that the user has the caps lock on.

    Case 2: A line edit which accept a normal string BUT as the last example, if the user write it in downcase, the line edit has to show it in uppercase. I tried with a inputmask but it still shows in down case, the cursor its extrange and it doent acept spaces

    Qt Code:
    1. lineedit_case2->setInputMask(">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
    To copy to clipboard, switch view to plain text mode 


    I tried it with:
    Qt Code:
    1. connect(lineedit_Num1, SIGNAL(textChanged(QString)), SLOT(toUpper(QString)));
    2.  
    3. void Test::toUpper(QString text)
    4. {
    5. QLineEdit *le = qobject_cast<QLineEdit *>(sender());
    6. if (!le)
    7. return;
    8. le->setText(text.toUpper()); // found this looking on internet but not sure about how it works
    9. lineedit_Num1->setText(text.toUpper()); //trying if that works
    10. }
    To copy to clipboard, switch view to plain text mode 
    Thank you so much
    Last edited by roseicollis; 2nd March 2015 at 08:52.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to make LineEdit show all text in uppercase

    If you only want the user to be able to type upper case letters, you can derive from QLineEdit and implement key event handling methods or install an event filter that converst lower case key events into respective upper case key events.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make LineEdit show all text in uppercase

    Hi anda_skoa,

    converst lower case key events into respective upper case key events.
    Thank you! I solved it with that. I let here the code for the case someone need it in the future
    Qt Code:
    1. if(key->key() >= Qt::Key_A && key->key() <= Qt::Key_Z &&
    2. ((key->modifiers() & Qt::ShiftModifier) == false))
    3. {
    4. QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyPress,
    5. key->key(), key->modifiers() | Qt::ShiftModifier,
    6. key->text().toUpper()));
    7. QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyRelease,
    8. key->key(), key->modifiers() | Qt::ShiftModifier,
    9. key->text().toUpper()));
    10. return true;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Any idea/help/example about the other problem of the validators?

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make LineEdit show all text in uppercase

    Quote Originally Posted by roseicollis View Post
    Hi anda_skoa,


    Thank you! I solved it with that. I let here the code for the case someone need it in the future
    Qt Code:
    1. if(key->key() >= Qt::Key_A && key->key() <= Qt::Key_Z &&
    2. ((key->modifiers() & Qt::ShiftModifier) == false))
    3. {
    4. QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyPress,
    5. key->key(), key->modifiers() | Qt::ShiftModifier,
    6. key->text().toUpper()));
    7. QApplication::sendEvent(obj, new QKeyEvent(QEvent::KeyRelease,
    8. key->key(), key->modifiers() | Qt::ShiftModifier,
    9. key->text().toUpper()));
    10. return true;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Any idea/help/example about the other problem of the validators?
    Are You sure it is working ? Try to press CapsLock and....

  5. #5
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make LineEdit show all text in uppercase

    Are You sure it is working ? Try to press CapsLock and....
    Well nice point... with CapsLock it works perfect (on adn off) BUT it doesn't if I press shift.... wtf O.o Now I'm totally lost... how can I make it work for both cases capslock and shit key?

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make LineEdit show all text in uppercase

    Solution from first post with slot toUpper() is very good. Why You don't use it ?

  7. #7
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make LineEdit show all text in uppercase

    I did but if I write: abcde that is what lineedit shows O.o, in downcase. and in fact, if I put a bp in the SLOT(toUpper(QString) it never gets there dunno why :S

    Edit: tee only case where the program shows downcase is if capslock is on but you press shift while write.
    Last edited by roseicollis; 2nd March 2015 at 16:37.

  8. #8
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: How to make LineEdit show all text in uppercase

    Try the following: just create a copy of the event with the Text property in uppercase version and pass it to the base implementation.

    Qt Code:
    1. void MyLineEdit::keyPressEvent( QKeyEvent* event )
    2. {
    3. event.accept();
    4.  
    5. QKeyEvent* modifiedEvent = new QKeyEvent( QEvent::KeyPress, event->key(), event->modifiers(), event->text().toUpper() );
    6. QLineEdit::keyPressEvent( modifiedEvent );
    7. }
    To copy to clipboard, switch view to plain text mode 
    EDIT: the same is needed for the release event handler.
    Last edited by Kryzon; 2nd March 2015 at 20:38.

  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make LineEdit show all text in uppercase

    Quote Originally Posted by roseicollis View Post
    I did but if I write: abcde that is what lineedit shows O.o, in downcase. and in fact, if I put a bp in the SLOT(toUpper(QString) it never gets there dunno why :S

    Edit: tee only case where the program shows downcase is if capslock is on but you press shift while write.
    You have something wrong in code. This solution works for sure - I use it myself.

  10. #10
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make LineEdit show all text in uppercase

    The mayus issue its solved Thank you so much! Any Idea about how to a RegExp which allows ABC0000 but not A or AB or ABC?

  11. #11
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: How to make LineEdit show all text in uppercase

    Quote Originally Posted by Kryzon View Post
    Try the following: just create a copy of the event with the Text property in uppercase version and pass it to the base implementation.

    Qt Code:
    1. void MyLineEdit::keyPressEvent( QKeyEvent* event )
    2. {
    3. event.accept();
    4.  
    5. QKeyEvent* modifiedEvent = new QKeyEvent( QEvent::KeyPress, event->key(), event->modifiers(), event->text().toUpper() );
    6. QLineEdit::keyPressEvent( modifiedEvent );
    7. }
    To copy to clipboard, switch view to plain text mode 
    To quote myself as I can't edit that post, instead of creating the event in the heap like that (which is flawed in that it should lead to a leak), creating it in the stack is the right way.

    Qt Code:
    1. QKeyEvent modifiedEvent( QEvent::KeyPress, event->key(), event->modifiers(), event->text().toUpper() );
    2. QLineEdit::keyPressEvent( &modifiedEvent );
    To copy to clipboard, switch view to plain text mode 
    The temporary event will be destroyed after the base implementation processes it.

  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make LineEdit show all text in uppercase

    Quote Originally Posted by Kryzon View Post
    Try the following: just create a copy of the event with the Text property in uppercase version and pass it to the base implementation.

    Qt Code:
    1. void MyLineEdit::keyPressEvent( QKeyEvent* event )
    2. {
    3. event.accept();
    4.  
    5. QKeyEvent* modifiedEvent = new QKeyEvent( QEvent::KeyPress, event->key(), event->modifiers(), event->text().toUpper() );
    6. QLineEdit::keyPressEvent( modifiedEvent );
    7. }
    To copy to clipboard, switch view to plain text mode 
    EDIT: the same is needed for the release event handler.
    This solution is not complete. What with copy/paste ?

  13. #13
    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: How to make LineEdit show all text in uppercase

    Quote Originally Posted by roseicollis View Post
    The mayus issue its solved Thank you so much! Any Idea about how to a RegExp which allows ABC0000 but not A or AB or ABC?
    Why not simply reimplement fixup() in the validator? Or even validate() itself. Just convert the text to upper case before exiting the function.

    As for your ABC thing, if the user can't enter anything else why make him input those letters at all? The easiest solution I can see is to replace the line edit with a spinbox, set a prefix and let the user enter numbers.
    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.


  14. #14
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make LineEdit show all text in uppercase

    Solved guys! Thank you so much! I just connected to the textChanged signal and look there for exactmatch()

Similar Threads

  1. Replies: 7
    Last Post: 5th February 2014, 11:20
  2. Why is QDialog.show() overriding my lineEdit text?
    By tescrin in forum Qt Programming
    Replies: 7
    Last Post: 19th June 2012, 19:57
  3. how to show a intger value in LineEdit
    By gauravg in forum Qt Programming
    Replies: 2
    Last Post: 28th February 2012, 10:01
  4. lineEdit's setSelection - How to show more text?
    By squidge in forum Qt Programming
    Replies: 3
    Last Post: 30th January 2010, 17:22
  5. Replies: 10
    Last Post: 12th February 2009, 07:23

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.