Nominate our 2022 Qt Champions!

How to trigger a signal when "File" or any other menu is clicked?

  • Hi All.I need to trigger an event when "File" menu is clicked" .Not the sub-menus which I can trigger via action but the menu itself. I have tried :

    connect(m_fileMenu,SIGNAL(triggered(QAction*)),this,SLOT(MenuClickSlot(QAction*)) );

    Doesn't work.

  • Moderators

    Clicking on a menu either shows it or hides it. There is no underlying action so there is no triggered() signal. You can connect to signals aboutToShow() and aboutToHide() to detect when it was clicked.

  • Can't I detect a click in the area of the whole menu bar? Or even better,can I detect a click on a whole area of the main window?Including any content it has got inside it?

  • Moderators

    You could, but that sounds strange to say the least. What are you trying to achieve with this actually?

  • Are you familiar with Adobe AfterEffects ? I am working on a program which should have similar interaction functionality.For example in AE if you click play the preview,it starts playing,but once you click somewhere inside the IDE the playback stops.That's what I want to have in my app.Thanks.

  • Moderators

    Ok, sounds like entirely different problem. The easiest would be to install event filter on the application object:

    class ClickHandler : public QObject {
    ClickHandler(QObject* parent) : QObject(parent) {}
    bool eventFilter(QObject * obj, QEvent * event) {
    if(event->type() == QEvent::MouseButtonRelease)
    //Do stuff
    return false;

    qApp->installEventFilter(new ClickHandler(qApp));

  • Ok,thanks for that.One more question.Will it override the event of the objects under the click?For instance,if the click location falls on a button?

  • Moderators

    No, filters do not override anything. They just allow you to block some events from reaching their target object. They do not replace the functionality implemented in that object's event handler (ok, they can if you block the event and handle it in the filter but you get the point).
    Blocking is done by returning true from the eventFilter method, but since in this case we don't want to block anything, just react to some of the events, we always return false.

Log in to reply