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?

Was it helpful?

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.

  1. Subclass QComboBox.
    Override keyPressEvent() : first call QComboBox::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.

  2. Implement a new class which inherits QObject. In this class, override eventFilter() : 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 your QComboBox. Connect a slot to this instance's signal, which you implemented.

If these are not clear, i recommend reading the following pages:

Using Custom Widgets with Qt designer

Qt Events & Event Filters

OTHER TIPS

You could also look into the activated( const QString& ) signal. It might be emitted when the user hits enter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top