tags:

views:

399

answers:

2

I am developing a QT application on redhat linux, I want to capture Carriage Return press event on QComboBox, I have written Signal for editTextChanged(), it is fired for every key press but not for Enter Key.. Why it is so? If there is any other way to capture carriage return..??

+2  A: 

I am assuming you wrote a slot& connected it to editTextChanged() signal of QComboBox. This signal is changed 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(). In the overridden keyPressEvent(), first call QComboBox::keyPressEvent() & 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(). In the 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 & 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

erelender
+1  A: 

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

Caleb Huitt - cjhuitt
this is just what I needed. Frustratingly editTextChanged() gets called before the other signals when the user changes selection, so there's no way to know /why/ the text has changed. If I got an activated() or a currentIndexChanged() signal before editTextChanged() then I could do something different... Alas.
dash-tom-bang