0
  1. There is editable QComboxBox. It handles all the functionality well except for the case when we edit/insert/delete character of a string in position other than end. e.g. "Hello" , if we delete 'e' cursor reaches at end.
  2. currently editTextChanged(string) signal handling is such that inside it setEditText(string) has been called which causes the cursor to reach at end.
  3. Have gone through some other stackoverflow forums where people have suggested to use -> saving initial cursor position , then call setEditText and then reset the cursor to saved position. This looks good but looks like workaround soln.
  4. Wanted to know whether there can be other way to i) call setEditText when focus has been lost.(QFocusEvent will not work since derived class is inherited from QObject not QWidget) or ii) call comboBox->lineEdit functionality to do setEditText. tried below but it doesn't work connect(qcomboBox->lineEdit(), SIGNAL(editingFinished()), this, SLOT(textChanged()));

Following are the links which I have gone through 1) editFinished for editable comboBox 2) saving cursor postion

Community
  • 1
  • 1

1 Answers1

0

You can install an event filter on the QComboBox and detect its focus in/out events that way. Look up QObject::installEventFilter in the documentation; it's pretty straightforward to use.

All of the events received by QComboBox will go through your class first, and you can decide what to do about them, if anything. That gives you the ability to change one class's behavior (i.e. the QComboBox) from another class (your's) without subclassing QComboBox.

goug
  • 2,294
  • 1
  • 11
  • 15
  • goug, I am somehow not able to call QObject::installEventFilter at right place. I have archive library(.a) where event filter method should be called. But installation can happen at the main application/library level. Tried installing the eventfilter at both app and library, but it does not work. Event filter function never gets hit. – user3795135 Nov 25 '16 at 14:10
  • I don't understand "installing at app and library". You install it on the object you want to monitor, and the parameter is a pointer to the object that will do the monitoring. So yes, you can call installEventFilter anywhere you want as long as you have pointers to both objects, it will work. Personally, I've tended to do it within the class I'm writing that will do the monitoring, but that's just convenience. – goug Nov 28 '16 at 19:53
  • The other mistake I've made once is that my declaration of the eventFilter method in the class I write was incorrect. It's a virtual function, and you're redefining it, so for that to work, the declaration must be exactly as documented. In my case, I accidentally added "const" to the end of it, so it never got called. – goug Nov 28 '16 at 19:54