<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[Solved]Priority of QKeySequence over keyPressEvent]]></title><description><![CDATA[<p dir="auto">Dear Qt-ers,<br />
I need to bind the Return key to a QAction. I have done this with<br />
@actionStartMeasure-&gt;setShortcut(Qt::Key_Return);@ Now I have some side-effects with both standard and custom widget.<br />
If the user press Enter when a QComboBox has focus, it will select the current entry in the combobox <em>and</em> trigger the QAction.<br />
If I create a new widget and reimplement keyPressEvent(), I never enter this function it when the Enter key is pressed.</p>
<p dir="auto">How can I deal with that. Is there some place where the priorities of shortcut handling are explained?<br />
I see I have a 3rd option: creating a QShortcut and connecting it to my QAction. Again: how are priorities handled?</p>
]]></description><link>https://forum.qt.io/topic/26051/solved-priority-of-qkeysequence-over-keypressevent</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Aug 2026 18:25:26 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/26051.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Apr 2013 08:12:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 12:04:27 GMT]]></title><description><![CDATA[<p dir="auto">Finally did that: @event-&gt;accept(); // Prevent event propagation to parents<br />
return false;    // Allow obj to handle the "Enter" event@</p>
<p dir="auto">Thanks <em>a lot</em> for your help raven-worx!</p>
]]></description><link>https://forum.qt.io/post/174460</link><guid isPermaLink="true">https://forum.qt.io/post/174460</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Fri, 19 Apr 2013 12:04:27 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 10:35:06 GMT]]></title><description><![CDATA[<p dir="auto">ah yes...sorry.<br />
Nevertheless did you try already try to accept the event and if necessary also return true in the event filter?</p>
]]></description><link>https://forum.qt.io/post/174443</link><guid isPermaLink="true">https://forum.qt.io/post/174443</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Fri, 19 Apr 2013 10:35:06 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 10:30:40 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="raven-worx" date="1366366885"]Best would be todo nothing of both :)<br />
You just want to inspect and trigger your action in case and don't want to manipulate the event chain at all.[/quote]No, in fact what I'm trying to do now is:</p>
<ul>
<li>Keep my "Return" QShortcut on a QAction</li>
<li>Prevent some specific widget types (eg. QLineEdit) to trigger this QAction when the user press Return in them</li>
</ul>
<p dir="auto">So my eventFilter won't trigger the action but "forbid triggering in some case" instead.</p>
]]></description><link>https://forum.qt.io/post/174441</link><guid isPermaLink="true">https://forum.qt.io/post/174441</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Fri, 19 Apr 2013 10:30:40 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 10:21:25 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="Julien M" date="1366366563"]<br />
If the object is not a QLineEdit, should I return false and reject the event? Or just return false?<br />
[/quote]<br />
Best would be todo nothing of both :)<br />
You just want to inspect and trigger your action in case and don't want to manipulate the event chain at all.</p>
<p dir="auto">[quote author="Julien M" date="1366366563"]<br />
Last question: In order to filter on receiver what is best in terms of perfomances.<br />
@obj-&gt;metaObject()-&gt;className()=="QLineEdit"<br />
QLineEdit* le = dynamic_cast&lt;QLineEdit*&gt;(obj)@[/quote]<br />
Best would be to hold pointers to your widgets and jsut do a pointer comparison. Thats the fastest way. Don't use dynamic_cast, only if you really have to, since the it has a large performance impact.</p>
]]></description><link>https://forum.qt.io/post/174438</link><guid isPermaLink="true">https://forum.qt.io/post/174438</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Fri, 19 Apr 2013 10:21:25 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 10:16:03 GMT]]></title><description><![CDATA[<p dir="auto">Thanks again, your help is greatly appreciated! I'm not sure why the doc for eventFilter() says<br />
[quote]In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.[/quote]<br />
Should I do both: accept it and return true?</p>
<p dir="auto">If the object is not a QLineEdit, should I return false and reject the event? Or just return false?</p>
<p dir="auto">Last question: In order to filter on receiver what is best in terms of perfomances.<br />
@obj-&gt;metaObject()-&gt;className()=="QLineEdit"<br />
QLineEdit* le = dynamic_cast&lt;QLineEdit*&gt;(obj)@</p>
]]></description><link>https://forum.qt.io/post/174437</link><guid isPermaLink="true">https://forum.qt.io/post/174437</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Fri, 19 Apr 2013 10:16:03 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 10:09:32 GMT]]></title><description><![CDATA[<p dir="auto">@event-&gt;accept() //stops event propagation@</p>
<p dir="auto">But in your code you don't check the watched object against your widget objects.</p>
]]></description><link>https://forum.qt.io/post/174434</link><guid isPermaLink="true">https://forum.qt.io/post/174434</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Fri, 19 Apr 2013 10:09:32 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 10:04:06 GMT]]></title><description><![CDATA[<p dir="auto">Ok in the mean time I tried to install in on the qApp instead of the QMainWindow.<br />
I now enter the eventFilter for all successive widgets that sees the keypressEvent propagation.<br />
How do I stop this propagation? Returning true does not stop it.<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/bool">@<bdi>bool</bdi></a> ApplicationMonitor::eventFilter(QObject *obj, QEvent *event)<br />
{<br />
if (event-&gt;type() == QEvent::ShortcutOverride )<br />
{<br />
QKeyEvent *ev = static_cast&lt;QKeyEvent *&gt;(event);<br />
if( ev-&gt;key()==Qt::Key_Return || ev-&gt;key()==Qt::Key_Enter )<br />
{<br />
qTrace(obj-&gt;metaObject()-&gt;className());<br />
return true;<br />
}<br />
}<br />
// standard event processing<br />
return QObject::eventFilter(obj, event);<br />
};@<br />
[quote]TRACE 19/04/13 12:07:33 - QDoubleSpinBox<br />
TRACE 19/04/13 12:07:35 - QWidget<br />
TRACE 19/04/13 12:07:35 - QDockWidget<br />
TRACE 19/04/13 12:07:35 - MainWindows[/quote]</p>
]]></description><link>https://forum.qt.io/post/174432</link><guid isPermaLink="true">https://forum.qt.io/post/174432</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Fri, 19 Apr 2013 10:04:06 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 09:52:43 GMT]]></title><description><![CDATA[<p dir="auto">did you install the main window on the widgets you want to inspect as an event filter?<br />
Alternatively as i said you can install the main window on the QApplication object and check there if the watched is one of your widgets.<br />
@qApp-&gt;installEventFilter(mainWin);@</p>
]]></description><link>https://forum.qt.io/post/174430</link><guid isPermaLink="true">https://forum.qt.io/post/174430</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Fri, 19 Apr 2013 09:52:43 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 09:27:43 GMT]]></title><description><![CDATA[<p dir="auto">Cool, that did the trick when I installed it on my QLineEdit.<br />
Would be could if I could install it once on the QMainWindow and check if the receiver is a QSpinBox, QComboBox or QLineEdit.<br />
However when I try that, the object returned in<br />
@eventFilter(QObject *obj, QEvent *event)@ is always a QMainWindow</p>
]]></description><link>https://forum.qt.io/post/174425</link><guid isPermaLink="true">https://forum.qt.io/post/174425</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Fri, 19 Apr 2013 09:27:43 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 08:38:04 GMT]]></title><description><![CDATA[<p dir="auto">You will only receive a ShortcutOverride event when there is a shortcut disambiguation.<br />
If no disambiguation is detected a normal shortcut event is sent and the action is triggered normally (as one would expected).<br />
Note: ShortcutOverride events aren't handled in Qt by default.</p>
<p dir="auto">I'm not 100% sure if your line edit will receive the shortcut override event, but i think so. At least it would make sense IMHO.<br />
But to make sure you can install an event filter on the QApplication and check which widget receives the shortcut override event initially.</p>
]]></description><link>https://forum.qt.io/post/174419</link><guid isPermaLink="true">https://forum.qt.io/post/174419</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Fri, 19 Apr 2013 08:38:04 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Fri, 19 Apr 2013 08:33:44 GMT]]></title><description><![CDATA[<p dir="auto">Thanks a lot for your answer.<br />
I played with shortcut context on my QAction, but it only work as intended with the default value.<br />
I will have a look at your suggestion.<br />
So you are telling me that my widget (in this case the QLineEdit) will receive a ShortcutOverride event when the Return key is pressed?<br />
Or that I should remove my "global" shortcut and listen to ShortcutOverride on the mainwindow instead?</p>
]]></description><link>https://forum.qt.io/post/174418</link><guid isPermaLink="true">https://forum.qt.io/post/174418</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Fri, 19 Apr 2013 08:33:44 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Thu, 18 Apr 2013 08:46:37 GMT]]></title><description><![CDATA[<p dir="auto">i think you need to listen to the QEvent::ShortcutOverride event (which is a QKeyEvent) and trigger your action manually.<br />
Did you additionally set a shortcut context of the shortcut set on your action?</p>
]]></description><link>https://forum.qt.io/post/174232</link><guid isPermaLink="true">https://forum.qt.io/post/174232</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Thu, 18 Apr 2013 08:46:37 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Thu, 18 Apr 2013 07:47:45 GMT]]></title><description><![CDATA[<p dir="auto">Still struggling with that question.</p>
]]></description><link>https://forum.qt.io/post/174214</link><guid isPermaLink="true">https://forum.qt.io/post/174214</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Thu, 18 Apr 2013 07:47:45 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Wed, 10 Apr 2013 09:08:06 GMT]]></title><description><![CDATA[<p dir="auto">I know how to install events. Other keystroke do work. The problem occurs with the "Return" key that is somehow eaten by the global shortcut. With your suggestion, you think I can keep my global shorcut and still receive "enter" event on my custom widget? I will try but I don't think so.<br />
Will end up with the same problem than when reimplementing keyPressEvent, won't I?</p>
]]></description><link>https://forum.qt.io/post/173173</link><guid isPermaLink="true">https://forum.qt.io/post/173173</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Wed, 10 Apr 2013 09:08:06 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Wed, 10 Apr 2013 08:46:56 GMT]]></title><description><![CDATA[<p dir="auto">You can install the eventFilter on your custom widget.</p>
<p dir="auto">bq. If I create a new widget and reimplement keyPressEvent(), I never enter this function it when the Enter key is pressed.</p>
<p dir="auto">For an example you can check "here":<a href="http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#eventFilter" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#eventFilter</a></p>
]]></description><link>https://forum.qt.io/post/173163</link><guid isPermaLink="true">https://forum.qt.io/post/173163</guid><dc:creator><![CDATA[Sam]]></dc:creator><pubDate>Wed, 10 Apr 2013 08:46:56 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Wed, 10 Apr 2013 08:31:11 GMT]]></title><description><![CDATA[<p dir="auto">-This way, i would not catch all the "Enter" events that have been accepted by child widgets?-<br />
Hum sorry I read too quickly.<br />
I don't understand: which widgets do you suggest me to install this filter on?</p>
]]></description><link>https://forum.qt.io/post/173159</link><guid isPermaLink="true">https://forum.qt.io/post/173159</guid><dc:creator><![CDATA[JulienMaille]]></dc:creator><pubDate>Wed, 10 Apr 2013 08:31:11 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Priority of QKeySequence over keyPressEvent on Wed, 10 Apr 2013 08:22:24 GMT]]></title><description><![CDATA[<p dir="auto">How about using an eventFilter() for your applicaiton where you can set newWidget-&gt;installEventFilters(this) and you can check based on the target value eg.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bool">@<bdi>bool</bdi></a> MainWindow::eventFilter(QObject *target, QEvent *event)<br />
{<br />
if (target == newWidget &amp;&amp;  event-&gt;type() == QEvent::KeyPress)<br />
{<br />
QKeyEvent *keyEvent = static_cast&lt;QKeyEvent *&gt;(event);<br />
if (keyEvent-&gt;key() == Qt::Key_Enter || keyEvent-&gt;key() == Qt::Key_Return)<br />
{<br />
//do something<br />
return QMainWindow::eventFilter(target,event);<br />
}<br />
}<br />
return QMainWindow::eventFilter(target,event);<br />
}@</p>
]]></description><link>https://forum.qt.io/post/173156</link><guid isPermaLink="true">https://forum.qt.io/post/173156</guid><dc:creator><![CDATA[Sam]]></dc:creator><pubDate>Wed, 10 Apr 2013 08:22:24 GMT</pubDate></item></channel></rss>