[SOLVED] Calling function each time qt windows gain focus


I have a mainwindow which in there is a Qtableview by clicking on insert record I go to other modal windows to add record when I add record and close the second windows I come back to main windows but the qtableview doesn’t show the new record that is added. The record is in database.

I already make this somehow work with :

    void MainWindow::showEvent( QShowEvent* event ) {
    QWidget::showEvent( event );

But it only works when windows get minimized.


Handling activate/deactivate events as follows will give you the desired behaviour

// overloading event(QEvent*) method of QMainWindow
bool MainWindow::event(QEvent* e)
    switch (e->type()) 
    case QEvent::WindowActivate:
    // gained focus
    //Update Table

    case QEvent::WindowDeactivate:
    // lost focus
    return QMainWindow::event(e);

Ref: https://gist.github.com/01walid/2276009

Answered By – hedgehog81

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *