Qt – Get a notification/event/signal when a Qt widget gets focus

focusqt

In Qt, when a widget receives focus, how can get a notification about it, so I can execute some custom code? Is there a signal or an event for that?

Best Solution

You can add en event filter.
This is an example of an application written with QtCreator. This form has a QComboBox named combobox.


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->comboBox->installEventFilter(this);
    .
    .
    .
}

bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::FocusOut)
    {
        if (object == ui->comboBox)
        {
            qWarning(object->objectName().toLatin1().data());
        }
    }
    return false;
}