Capturing text change events in a QComboBox
Question
I am developing a Qt application on Red Hat Linux. I want to capture Carriage Return key press events in a QComboBox
.
I have connected a slot to the signal editTextChanged()
which is emitted for every key press but not for the Enter Key.
Why so? Is there any other way to detect Carriage Returns?
Solution
I am assuming you wrote a slot and connected it to QComboBox::editTextChanged()
signal.
This signal is fired when the text changes and Enter does not change the text, it accepts it. If you want to capture Carriage Return, there are a number of ways you can follow.
Subclass
QComboBox
.
OverridekeyPressEvent()
: first callQComboBox::keyPressEvent()
and then check if the pressed key is Enter. If it is, emit a signal.
Use this subclass whenever you need. Search about promoting widgets in QDesigner if you don't know how.Implement a new class which inherits
QObject
. In this class, overrideeventFilter()
: check if the event is a key press. If it is, check if it is the Enter key. If it is, emit a signal.
Then, create an instance of this class and set it as event filter to yourQComboBox
. Connect a slot to this instance's signal, which you implemented.
If these are not clear, i recommend reading the following pages:
OTHER TIPS
You could also look into the activated( const QString& )
signal. It might be emitted when the user hits enter.