You can also use a QValidator to force input to upper case:
Qt Code:
{ Q_OBJECT public: { } { input = input.toUpper(); return Acceptable; } };To copy to clipboard, switch view to plain text mode
You can also use a QValidator to force input to upper case:
Qt Code:
{ Q_OBJECT public: { } { input = input.toUpper(); return Acceptable; } };To copy to clipboard, switch view to plain text mode
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 07:20.
Because you are modifying an event which already has been executed.BTW, why doesn't the above code I tried to use work?
For the modifier to have an effect you need to post an event which contains it.
You can add discrimination code in your slot to use Qstring::toUpper() only when you need it.If I will use the signal editTextChanged(QString), then it will capitalize everything,
==========================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.
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!
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.
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?
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.
I wouldn't. And if I had to, I would craft my own events from scratch.How would you set an event for an object to simulate special key presses if not with keyPressEvent?
Again, the event handler deals with an event which already happened - in the past - you can't change what already happened.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.
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.
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.![]()
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.
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:
protected: { if (e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z) emit KeyPressed(e->text().toUpper().at(0)); else } signals: void KeyPressed(const QChar&);To copy to clipboard, switch view to plain text mode
Bookmarks