Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

widget "leave" event /"focus out" event



  • Hello,
    [Im using QT 5.13.1 on Win 10 64bit with VS plugin 2.4.2]
    I have a groupbox containing several spinboxes. My intension is that I get a message box when groupebox losses the focus (if values in the spinboxes are changed since entering the groupbox (focus in)).
    a) Since a QGroubBox is also a QWidget and this should have the events "focus in" and "focus out" I am wondering why they are not fired.
    b) As a workaround of a) I use the "leave" event instead, but in my eventfilter I get an infinite loop of leave events and finaly a program crash. The reason is that the message box that I open causes the groupbox to fire a leave event again but the event was already fired otherwise I would not be in the eventfilter.

    bool Application::eventFilter(QObject* object, QEvent* event)
    {
    printf("event = %d\n", event->type());
    if ((object == groupbox) && (event->type() == QEvent::Leave)) // FocusOut is not fired so can't use it here
    {
    // event->accept(); // uncomment this line does not help
    // qApp->processEvents(); // uncomment this line does not help
    if (data changed)
    {
    int btn = QMessageBox::question(...);
    if (btn == QMessageBox::Ok)
    slot_roi_pb_accept();
    if (btn == QMessageBox::No)
    slot_roi_pb_cancel();
    }
    }
    return QWidget::eventFilter(object, event); // apply default filter
    }

    My questions:

    • Why does a groupebox does not fire events like "focus in" and "focus out"?
    • How can an object be left when it is already left when it is not entered in between?
    • Is there any known solution for my problem?

    BTW: The leave event is fired at the wrong time when leaving the groupebox with a pressed mouse key. It is fired when releasing the mouse key and not when leaving the area of the widget!!! Why?

    Thanks a lot


  • Lifetime Qt Champion

    Hi,

    Because there's nothing to focus on a QGroupBox. Moving the mouse over a widget won't give it focus unless you made it so.

    However, your design is a bit surprising, why not use the valueChanged signal of the QSpinBox to detect a change ?



  • Hello SGaist,
    I did not expect to have or lose focus when only moving the mouse over it but when I have the focus in a spinbos inside the groupebox and then I click somewhere outside I would expect that spinbox and all parents lose this focus.
    In my opinion a relation of objects in child parent hirachie like the controls we are talking about should behave like this. If a spinbox inside the area of a groupebox but not being a child of it (not nested) losses focus I would not expect that groupebox losses also a focus but in the case I have I expact that.

    My design is so "suprising" because I have several spinboxes and I dont want to validate each of this seperatly but at a certain time all together. In fact I have slots for all of them connected to valueChanged(int) but I do not want to get a confirm dialog for each value changed.


  • Lifetime Qt Champion

    Why would the parent lose a focus it didn't have in the first place ?

    In any case, with your additional explanations, I understand better what you want which makes it way less "surprising".

    Are all your controls grouped in group boxes ?


Log in to reply