Important: Please read the Qt Code of Conduct -

How to make QDialog receive all mouse and keyboard event inside it?

  • I want to make a QDialog close(call accept()) when I not do action in two minute. I already set QThread and QTimer to the QDialog.

    But I do not know how to make QTimer stop when some action occur(mouse event and keyboard event). The QDialog can not receive the child widgets' event. How can I receive all mouse and keyboard event inside the QDialog and not affect the child widgets to handle their events?

  • Implement a EventFilter for the Dialog and when a mouse/keuboard occurs set a flag variable.
    When the timer fires, if the flag is set, reset it and restart the timer (you can use a single shoot timer for that instead)
    If the flag is not set close the dialog.

    Doesn't work !!
    I have no clue except to install a event filter for QApplication
    It is over complicated, see this

    Maybe you can deal with the move/leave/enter events at the window level ?

  • Lifetime Qt Champion


    You can make your dialog the filter of its child widgets and on mouse/keyboard events, restart the timer. As already suggested make the timer a single shot.

  • Lifetime Qt Champion

    Why not do it reverse?
    You pop the dialog + semi transparent overlay widget saying "Auto config"
    user can then click the overlay to change the setting or let it auto-accept.
    That way its clear it will auto accept and its much, much simpler to handle code wise.

  • @mpergand Thanks for your help.

    I read the answer on stackoverflow.QCoreApplication::instance()->installEventFilter(this); this code is helpful to get event even inside the child widgets.

    I do not set such flag. I just stop and start the thread of the timer when the event filter get the key or mouse event.

Log in to reply