Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, if i understood right. </p> <p>You are letting Qt handle the right button ( you don't check Context in your slot ) and then menu disappears when loses focus. </p> <p>But youre handling left button clicks and you are not able to get the same behavour. Right?</p> <p>I can think of, at least, two ways to do this through an eventFilter:</p> <p>This would be a way to make a fake event.</p> <pre><code>bool MainWindow::eventFilter(QObject *obj, QEvent *event){ if (event-&gt;type() == QEvent::MouseButtonPress) { QMouseEvent *mEvent = static_cast&lt;QMouseEvent *&gt;(event); if(mEvent-&gt;button() == Qt::LeftButton) { QMouseEvent my_event = new QMouseEvent ( mEvent-&gt;type(), mEvent-&gt;pos(), Qt::Rightbutton , mEvent-&gt;buttons(), mEvent-&gt;modifiers() ); QCoreApplication::postEvent ( trayIcon, my_event ); return true; } } return QObject::eventFilter(obj, event); } </code></pre> <p>Install with</p> <pre><code>trayIcon-&gt;installEventFilter(this); </code></pre> <p>from Main Window</p> <p>But, i think it's a bit tricky.</p> <p>If you're making an eventFilter, you could watch for focusOut event on menu.</p> <pre><code>bool MainWindow::eventFilter(QObject *obj, QEvent *event){ QMouseEvent *mEvent = dynamic_cast&lt;QMouseEvent *&gt;(event); if(mEvent) { if(mEvent-&gt;type() == QEvent::Leave || mEvent-&gt;type() == QEvent::WindowDeactivate) { trayIcon-&gt;contextMenu()-&gt;close(); return true; } } return QObject::eventFilter(obj, event); } </code></pre> <p>Note that i havent tried. But should be worth a try.</p> <p>Some handy links:</p> <p><a href="https://stackoverflow.com/questions/14147304/how-to-detect-that-my-application-lost-focus-in-qt">How to detect that my application lost focus in Qt?</a></p> <p><a href="http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter" rel="nofollow noreferrer">installEventFilter</a></p> <p><a href="http://qt-project.org/doc/qt-4.8/qcoreapplication.html#postEvent" rel="nofollow noreferrer">postEvent</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload