35

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?

sashoalm
  • 75,001
  • 122
  • 434
  • 781

8 Answers8

39

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;
}
fat
  • 6,435
  • 5
  • 44
  • 70
17

There is a "focusChanged" signal sent when the focus changes, introduced in Qt 4.1.
It has two arguments, he widget losing focus and the one gaining focus:

void QApplication::focusChanged(QWidget * old, QWidget * now)
harrymc
  • 1,069
  • 8
  • 33
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 2
    For reference, here is the function in Qt docs: http://doc.qt.io/qt-5/qapplication.html#focusChanged – Hawkins Feb 21 '18 at 20:23
13

Qt Designer isn't designed for this level of WYSIWYG programming.

Do it in C++:

class LineEdit : public QLineEdit
{
    virtual void focusInEvent( QFocusEvent* )
    {}
};
mxcl
  • 26,392
  • 12
  • 99
  • 98
  • what is meant by WYSIWYG programming ? – AAEM May 02 '18 at 02:20
  • WYSIWYG : What You See Is What You Get. This is what MS Word tries. LaTex for example is "WYSIWYM" - What You See Is What You Mean - the produced pdf differs from the text/code you wrote. – KYL3R May 31 '18 at 11:31
7

The simplest way is to connect a slot to the QApplication::focusChanged signal.

2

I'd have to play with it, but just looking at the QT Documentation, there is a "focusInEvent". This is an event handler.

Here's how you find information about.... Open up "QT Assistant". Go to the Index. Put in a "QLineEdit". There is a really useful link called "List of all members, including inherited members" on all the Widget pages. This list is great, because it even has the inherited stuff.

I did a quick search for "Focus" and found all the stuff related to focus for this Widget.

Bob
  • 456
  • 4
  • 4
2

You have hit on of the weird splits in QT, if you look at the documentation focusInEvent is not a slot it is a protected function, you can override it if you are implementing a subclass of your widget. If you you just want to catch the event coming into your widget you can use QObject::installEventFilter it let's you catch any kind of events.

For some odd reason the developers of Trolltech decided to propagate UI events via two avenues, signals/slots and QEvent

Harald Scheirich
  • 9,676
  • 29
  • 53
  • 1
    You can override a virtual base function. That is why events are virtual functions. Trolltech use signals instead when inheritance for that functionality is not likely to be useful. – mxcl Dec 02 '08 at 23:41
  • Events and Signal/Slots are quite different. For example, the order in which receivers receive events is clearly defined, and processing is stopped once the event is handled. – Frank Osterfeld Jan 19 '11 at 07:05
2

Just in case anybody looking for two QMainWindow focus change . You can use

if(e->type() == QEvent::WindowActivate)
{
    //qDebug() << "Focus IN " << obj << e ;

}
Yash
  • 6,644
  • 4
  • 36
  • 26
0

QWidget::setFocus() is slot, not signal. You can check if QLineEdit is in focus with focus property. QLineEdit emits signals when text is changed or edited, see documentation.