<div dir="ltr"><div class="">
<p>I'm trying to use QShortcut to set a keyboard shortcut, only for when a certain widget is focused.</p>
<p>I thought that by setting the parent, this would work. For example:</p>
<pre style class=""><code><span class="">QtGui</span><span class="">.</span><span class="">QShortcut</span><span class="">(</span><span class="">QtGui</span><span class="">.</span><span class="">QKeySequence</span><span class="">(</span><span class="">"Ctrl+Return"</span><span class="">),</span><span class=""> self</span><span class="">.</span><span class="">myWidget</span><span class="">,</span><span class=""> self</span><span class="">.</span><span class="">doSomething</span><span class="">)</span></code></pre>
<p>But, this is fired when ever Ctrl+Return is pressed. I know I can do this by subclassing <code>QWidget</code> and overwriting <code>setShortcut</code>, but this seems overkill?</p>
<p>Is there an easier way?</p><p></p>
<p>I think this can be done using <code>QShortcutContext</code> <a href="http://pyqt.sourceforge.net/Docs/PyQt4/qshortcut.html" rel="nofollow">http://pyqt.sourceforge.net/Docs/PyQt4/qshortcut.html</a> but </p>
<pre style class=""><code><span class="">QtGui</span><span class="">.</span><span class="">QShortcut</span><span class="">(</span><span class="">QtGui</span><span class="">.</span><span class="">QKeySequence</span><span class="">(</span><span class="">"Ctrl+Return"</span><span class="">),</span><span class=""> self</span><span class="">.</span><span class="">myWidget</span><span class="">,</span><span class=""> self</span><span class="">.</span><span class="">doSomething</span><span class="">,</span><span class=""> context</span><span class="">=</span><span class="">QtCore</span><span class="">.</span><span class="">Qt</span><span class="">.</span><span class="">WidgetShortcut</span><span class="">)</span></code></pre>
<p>doesn't work either.</p>
</div><div><br>Thanks,<br>Joseph David Borġ <br></div>
</div>