How to catch QComboBox popup close event

pau picture pau · Oct 7, 2013 · Viewed 7.2k times · Source

A am using QComboBox derived class to show my items. My combo box is read only. But how can I catch the event when popup view of combo box closes?.
For example, when user clicks a mouse button somewhere out of my combo box?
Thank you very much in advance.

Answer

Sebastian Lange picture Sebastian Lange · Oct 7, 2013

What for do you want this event? If the QComboBox closes without selection nothing changed. The signals given will only be activated when a selection has been made.

If you insist on reading a "close-event", you could subclass focusOutEvent(QFocusEvent*) or use an event handler for the focus out event and emit a custom signal. Eventually you want to have a boolean flag set on hadEditFocus() before, so you can see if the dropdown would be opened.

Edit: Eventually it would be easier to subclass and reimplement showPopup() and hidePopup() as:

void MyClass::showPopup() 
{
  QComboBox::showPopup();
  emit signalPopupShown();
}

void MyClass::hidePopup()
{
  QComboBox::hidePopup();
  emit signalPopupHidden();
}

but I am not sure if hidePopup() gets called on focus-loose.