1

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?

ymoreau
  • 3,402
  • 1
  • 22
  • 60
Muhammad Ummar
  • 3,541
  • 6
  • 40
  • 71

2 Answers2

4

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

ymoreau
  • 3,402
  • 1
  • 22
  • 60
erelender
  • 6,175
  • 32
  • 49
3

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

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
  • 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 Jan 19 '10 at 21:47