Extract signal if all child widgets lose focus

I have QStackedWidgetone that contains several pages filled by various children QLineEditand QComboBox. I want to emit a signal whenever there is QStackedWidgetno longer any child with focus (given that the child was focused first). Therefore, the transition from the child to the child will not give a signal, but as soon as the widget is selected outside QStackedWidget, a signal is issued. Any tips on how to implement this? I looked at InstallEventFilterand QSignalMapper, but not one of them fits my needs. Any advice would be appreciated.

+5
source share
2 answers

This is a bit complicated. Even with the cyclic transition from child to child, there is a short period after the child loses focus and before the other child gains focus, where not a single child from a complex widget has focus. Although this condition is extremely short-lived, it is a fact.

The only parameter that I see is to decide how long the period between the child widgets loses focus and the absence of the child widget that you think is long enough to state that the child widget is actually no longer in focus. This will be a mechanism similar to what is used to highlight two mouse clicks with one click.

, , . (100 ?) , . , , . , , , .

+2

QApplication:: focusChanged, . - QStackedWidget:

class StackedFocusWidget : public QStackedWidget {

    Q_OBJECT

public:

    StackedFocusWidget(QWidget *parent = 0) : QStackedWidget(parent) {
        connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)), this, SLOT(onFocusChanged(QWidget *, QWidget *)));
    }

private slots:

    void onFocusChanged(QWidget *old, QWidget *now) {
        bool focusOld = old != 0 && isAncestorOf(old);
        bool focusNow = now != 0 && isAncestorOf(now);
        if (!focusOld && focusNow) {
            emit gotFocus();
        } else if (focusOld && !focusNow) {
            emit lostFocus();
        }
    }

signals:

    void gotFocus();
    void lostFocus();
};

StackedFocusWidget:: gotFocus StackedFocusWidget:: lostFocus , .

+4

All Articles