Results 1 to 13 of 13

Thread: How to force capital letters in input field?

  1. #1
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to force capital letters in input field?

    I have created a subclass of QComboBox where I want to catch keys from 'a' to 'z' and manually force them to capital letters 'A'-'Z'. It has to be done real-time like caps lock was pressed.

    I'm trying the following approach w/o success:

    Qt Code:
    1. void CommandBox::keyPressEvent(QKeyEvent *e) {
    2. if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
    3. emit enterPressed(currentText());
    4. } else if (e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z) {
    5. if (capital) e->setModifiers(Qt::ShiftModifier);
    6. }
    7. qDebug() << e->key() << e->modifiers();
    8. QComboBox::keyPressEvent(e);
    9. }
    To copy to clipboard, switch view to plain text mode 

    The modifier is set, I can see it in the printout of qDebug(), but the character is still lower case in the field.
    Any guess?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to force capital letters in input field?

    do you want to force the user to input only capitals, or just to capitalize any input?
    If the later - have a look at QString::toUpper().
    And instead of sub classing you can simply catch the editTextChanged() signal.
    Last edited by high_flyer; 15th March 2012 at 22:36.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    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: How to force capital letters in input field?

    You can also use a QValidator to force input to upper case:
    Qt Code:
    1. class UpperCaseValidator : public QValidator
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit UpperCaseValidator(QObject *parent = 0): QValidator(parent)
    6. { }
    7.  
    8. QValidator::State validate( QString& input , int& pos ) const
    9. {
    10. input = input.toUpper();
    11. return Acceptable;
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to force capital letters in input field?

    Thanks for the tips!
    I will have to create a subclass, because of other reason - to handle the event when user presses Enter (it will be a run command box basically for a terminal program).
    There is a QCheckBox next to the modified ComboBox, so this way the user can turn on automatic capitalization or turn it off if he connects to standard telnet connection that accepts lower case inputs.
    BTW, why doesn't the above code I tried to use work? I thought if I modified the e event and I pass it to the original QComboBox class, it would accept it the way as if the user pressed the shift key as well.

    If I will use the signal editTextChanged(QString), then it will capitalize everything, however there are times when lower cased alphabets need to be used. Capitalization is used only to help the user so he doesn't have to press caps lock all the time, because lower cases usage is very rare in this special terminal connection to give commands. Should I store the original text and check if the user added a new character and only modify that one, and if he deletes then skip it? It looks quite cumbersome.
    Last edited by falconium; 16th March 2012 at 08:20.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to force capital letters in input field?

    BTW, why doesn't the above code I tried to use work?
    Because you are modifying an event which already has been executed.
    For the modifier to have an effect you need to post an event which contains it.

    If I will use the signal editTextChanged(QString), then it will capitalize everything,
    You can add discrimination code in your slot to use Qstring::toUpper() only when you need it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to force capital letters in input field?

    Quote Originally Posted by high_flyer View Post
    Because you are modifying an event which already has been executed.
    For the modifier to have an effect you need to post an event which contains it.
    So does it mean setModifiers is totally useless or I would need to call it somewhere before keyPressEvent is called? If the latter, how could I do that? Do I need to recall keyPressEvent recursively?

    I have too many questions, I know, but I don't get the logic of it.

    Thanks!

  7. #7
    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 force capital letters in input field?

    setModifiers() only sets the modifier. It doesn't change what QKeyEvent::text() returns. You would really do yourself a favour by using a validator as Chris suggested. If you want to intercept return presses, an editable QComboBox has an internal QLineEdit instance which has a returnPressed() signal you can intercept.
    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.


  8. #8
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to force capital letters in input field?

    Alright, I see, but I still don't get it why it works if the user presses e.g. Shift-A, and why doesn't it only pass QKeyEvent::text(), but the key() as well to to QComboBox input field.
    I will need to use customized class of QComboBox, because I have more signals included for special key combinations.

    Anyway, let's forget about that the input in this case is a QComboBox. How would you set an event for an object to simulate special key presses if not with keyPressEvent?

  9. #9
    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 force capital letters in input field?

    Quote Originally Posted by falconium View Post
    Alright, I see, but I still don't get it why it works if the user presses e.g. Shift-A
    Because at the time when the event is created, Shift is already pressed.

    By the way, your approach will be totally usless in many situations, e.g. when someone pastes in a lowercase text instead of typing it manually. With a validator it would have worked properly.

    How would you set an event for an object to simulate special key presses if not with keyPressEvent?
    I wouldn't. And if I had to, I would craft my own events from scratch.
    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.


  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to force capital letters in input field?

    Alright, I see, but I still don't get it why it works if the user presses e.g. Shift-A, and why doesn't it only pass QKeyEvent::text(), but the key() as well to to QComboBox input field.
    Again, the event handler deals with an event which already happened - in the past - you can't change what already happened.
    The data you get is about the event which already happened, and was processed.
    When you press a key the hardware generates an interrupt, which is translated to the correct event by the OS, which is sent to Qt to act on it.
    Changing the event data will not go all the way back to the keyboard.
    Again - you can create synthetic events by posting your own events.
    But that would be the wrong approach in your case, IMHO.
    All the three of us are pointing you to other ways yet you insist on the event posting approach.
    Well - be my guest.
    Have a look at QCorApplication::postEvents(), and QCoreApplication::sendEvent(), and the QEvent, on how to create and post custom events.

    EDIT:
    beat to it by wystoa
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to force capital letters in input field?

    Thanks for all the explanations!

    To help others in the future, let me tell how I successfully solved it by a workaround solution.
    If user wants to have automatic capitalization and standard alphabet key is pressed between A and Z, then the modified keyPressEvent emits a signal including the key as parameter and returns without passing the event to the inherited class QComboBox.
    In the main application I connect this signal to a slot that adds the upper case of the pressed key to the currentText() of the inherited QComboBox object.

    Simple, as always.

  12. #12
    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 force capital letters in input field?

    Just please tell us why you didn't want to use the validator approach. You could have written about 5-10 lines of code to do what you wanted but you decided to write many more lines of code that don't do what you wanted (see my point on pasting text into the box). If the user wants to type in capital letters then it will be easier for him to enable caps lock than to check some boxes on your ui.
    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.


  13. #13
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to force capital letters in input field?

    I've used the same system: sending a signal when a key has been pressed.

    I need to capitalise all the user inputs but not what the gui automatically inserts, so a signal is much more efficient than a validator.

    And it's 10 lines

    Qt Code:
    1. protected:
    2. void keyPressEvent(QKeyEvent* e)
    3. {
    4. if (e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z)
    5. emit KeyPressed(e->text().toUpper().at(0));
    6. else
    7. QLineEdit::keyPressEvent(e);
    8. }
    9. signals:
    10. void KeyPressed(const QChar&);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Twisted Letters in Vertical Axis Title
    By DrunkenUFOPilot in forum Qwt
    Replies: 6
    Last Post: 25th January 2012, 00:56
  2. QregExp: matching accented letters
    By bred in forum Qt Programming
    Replies: 0
    Last Post: 4th January 2011, 11:48
  3. A collection of letters and numbers
    By NewLegend in forum Qt Programming
    Replies: 8
    Last Post: 8th September 2010, 20:44
  4. Replies: 11
    Last Post: 11th July 2010, 22:44
  5. Reading umlaut letters from sqlite database
    By Djony in forum Qt Programming
    Replies: 11
    Last Post: 17th November 2006, 11:30

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.