I have a QStackedWidget
which holds several pages full of various QLineEdit
and QComboBox
children. I want to emit a signal whenever the QStackedWidget
no longer has any child with focus (given that a child had focus to begin with). So moving from child to child will not emit a signal, but once a widget is selected outside of the QStackedWidget
, a signal is emitted. Any advice on how to implement this? I've looked at InstallEventFilter
and QSignalMapper
, but neither of those appear to fit my needs. Any advice would be appreciated.
You can connect to the QApplication::focusChanged signal in order to evaluate the focus widgets within a corresponding slot. The clean way to do this is to derive from 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();
};
The signals StackedFocusWidget::gotFocus and StackedFocusWidget::lostFocus will be emitted whenever the stacked widget or any of its childs receives or loses focus.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With