<?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[Why keyPressEvent doesn&#x27;t work?]]></title><description><![CDATA[<p dir="auto">Hi everyone.</p>
<p dir="auto">I wrote a simple eventFilter, and it works well but I don't understand why a keyPressEvent isn't working.<br />
Indeed, the case Key_Tab works perfectly, but the case Key_Backtab doesn't.<br />
I have testing it and the problem resides on keyPressEvent(keyEvent).<br />
My code is associated with a QComboBox, and particularly it lightens rows forwards but not backwards (in this case the new row is not visually updated).</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bool">@<bdi>bool</bdi></a> eventFilter(QObject *t, QEvent *e)<br />
{<br />
QKeyEvent <em>keyEvent = static_cast&lt;QKeyEvent</em>&gt;(e);<br />
int key = keyEvent-&gt;key();</p>
<pre><code>    if(e-&gt;type() == QEvent::KeyPress)
    {
        switch(key)
        {
            case Qt::Key_Backtab:
            //case Qt::Key_Up:
            {
                int curIndex = this-&gt;currentIndex();
                int nextIndex;

                if (this-&gt;currentIndex() == 0)
                    nextIndex = this-&gt;count() -1;
                else {
                    nextIndex = curIndex - 1;
                }

                this-&gt;setCurrentIndex(nextIndex);

                QComboBox::keyPressEvent(keyEvent);

                break;
            }
            case Qt::Key_Tab:
            //case Qt::Key_Down:
            {
                int curIndex = this-&gt;currentIndex();
                int nextIndex;

                if (this-&gt;currentIndex() == this-&gt;count() -1 )
                    nextIndex = 0;
                else {
                    nextIndex = curIndex + 1;
                }

                this-&gt;setCurrentIndex(nextIndex);

                QComboBox::keyPressEvent(keyEvent);

                break;
            }
        }
    }@
</code></pre>
]]></description><link>https://forum.qt.io/topic/47344/why-keypressevent-doesn-t-work</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 11:46:35 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/47344.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 22 Oct 2014 18:42:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Fri, 24 Oct 2014 09:50:25 GMT]]></title><description><![CDATA[<p dir="auto">In this case Backtab is not recognized any more by my machine. And therefore the if statement doesn't compile.</p>
<p dir="auto">But any way, I think that I expressed badly my problem. My keyPress already works. My problem is simply that:</p>
<p dir="auto">when I setCurrentIndex() in my comboBox, in case the event is Backtab, the highlight in the comboBox doesn't move.</p>
<p dir="auto">It is simply just the highlight, because all the rest works. Even setCurrentIndex() I am sure works. But:</p>
<p dir="auto">Tab -&gt; highlight (in comboBox) moves forwards<br />
Backtab -&gt; highlight doesn't move</p>
]]></description><link>https://forum.qt.io/post/248875</link><guid isPermaLink="true">https://forum.qt.io/post/248875</guid><dc:creator><![CDATA[lucadanieli]]></dc:creator><pubDate>Fri, 24 Oct 2014 09:50:25 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Fri, 24 Oct 2014 07:31:11 GMT]]></title><description><![CDATA[<p dir="auto">I suggest:<br />
@void MainWindows::keyPressEvent(QKeyEvent *e)</p>
<p dir="auto">{</p>
<pre><code>if (e-&gt;type() == QEvent::KeyPress)
</code></pre>
<p dir="auto">{<br />
QKeyEvent <em>keyEvent = static_cast&lt;QKeyEvent</em>&gt;(e);</p>
<pre><code>int keyInt = keyEvent-&gt;key();



Qt::KeyboardModifiers modifiers = keyEvent-&gt;modifiers();
</code></pre>
<p dir="auto">//For combination of button<br />
if(modifiers &amp; Qt::ShiftModifier)<br />
keyInt += Qt::SHIFT;<br />
if(modifiers &amp; Qt::ControlModifier)<br />
keyInt += Qt::CTRL;<br />
if(modifiers &amp; Qt::AltModifier)<br />
keyInt += Qt::ALT;<br />
if(modifiers &amp; Qt::MetaModifier)<br />
keyInt += Qt::META;</p>
<pre><code>if(keyInt==Qt::Key_Backtab)
    on_spacePushButton_Backtab();
if(keyInt==Key_Tab)
    on_pushButtonTab();

}
</code></pre>
<p dir="auto">}</p>
<p dir="auto">void MainWindows::on_spacePushButton_Backtab()<br />
{<br />
.......................<br />
........................<br />
}</p>
<p dir="auto">void MainWindows::on_pushButtonTab()<br />
{<br />
.......................<br />
........................<br />
}</p>
<p dir="auto">@</p>
]]></description><link>https://forum.qt.io/post/248861</link><guid isPermaLink="true">https://forum.qt.io/post/248861</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 24 Oct 2014 07:31:11 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Thu, 23 Oct 2014 14:28:32 GMT]]></title><description><![CDATA[<p dir="auto">Thank Salvatello for your suggestion. Did you mean to override the default keyPressEvent? Like this:</p>
<p dir="auto">@void keyPressEvent(QKeyEvent *e)<br />
{<br />
int key = e-&gt;key();</p>
<pre><code>    switch(key)
    {
        case Qt::Key_Backtab:
        //case Qt::Key_Up:
        {
            int curIndex = this-&gt;currentIndex();
            int nextIndex;

            if (this-&gt;currentIndex() == 0)
                nextIndex = this-&gt;count() -1;
            else {
                nextIndex = curIndex - 1;
            }

            this-&gt;setCurrentIndex(nextIndex);

            break;

        }
        case Qt::Key_Tab:
        //case Qt::Key_Down:
        {
             int curIndex = this-&gt;currentIndex();
             int nextIndex;

             if (this-&gt;currentIndex() == this-&gt;count() -1 )
                  nextIndex = 0;
             else {
                  nextIndex = curIndex + 1;
             }

             this-&gt;setCurrentIndex(nextIndex);

             break;
        }
    }
}

bool eventFilter(QObject *t, QEvent *e)
{
    QKeyEvent *keyEvent = static_cast&lt;QKeyEvent*&gt;(e);
    int key = keyEvent-&gt;key();

    if(e-&gt;type() == QEvent::KeyPress)
    {
        this-&gt;keyPressEvent(keyEvent);
    }
</code></pre>
<p dir="auto">@</p>
<p dir="auto">This code still changes documents in the editor, but it doesn't move the highlight in the menu list forwards and backwards any more.</p>
<p dir="auto">If I add</p>
<p dir="auto">@         QComboBox::keyPressEvent(e); @</p>
<p dir="auto">just after the switch function, I obtain the same behaviour as before, so: documents changed in both directions; selection highlights only if forward direction.</p>
<p dir="auto">Therefore now my question is more specific: should setCurrentIndex() automatically change the highlight in the menu list to the specific row called?<br />
(If I am not wrong: when calling QComboBox::keyPresseEvent() what I do is asking for the default behaviour of the comboBox relative to Key_Tab. Maybe comboBox has not default behaviour for Key_Backtab? How to lighten manually a row then?)</p>
]]></description><link>https://forum.qt.io/post/248787</link><guid isPermaLink="true">https://forum.qt.io/post/248787</guid><dc:creator><![CDATA[lucadanieli]]></dc:creator><pubDate>Thu, 23 Oct 2014 14:28:32 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Thu, 23 Oct 2014 13:09:45 GMT]]></title><description><![CDATA[<p dir="auto">Why do not you try to do the opposite?<br />
that is, to call a function keypress (which will change the editor or combobox)?</p>
]]></description><link>https://forum.qt.io/post/248769</link><guid isPermaLink="true">https://forum.qt.io/post/248769</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Thu, 23 Oct 2014 13:09:45 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Thu, 23 Oct 2014 11:09:48 GMT]]></title><description><![CDATA[<p dir="auto">I am not sure, but if I just delete both these lines from the Backtab case:</p>
<p dir="auto">@                 this-&gt;setCurrentIndex(nextIndex);</p>
<pre><code>                QComboBox::keyPressEvent(keyEvent);
</code></pre>
<p dir="auto">@<br />
nothing happens any more, and neither documents are any more changing backwards in the Editor (instead, the Tab case still works): completely frozen.</p>
<p dir="auto">So I don't think it is used by anything else, also considering that I have focus on the menu list.</p>
<p dir="auto">Is it possible that Backtab (recognized as modifier + tab) wants keyEvent-&gt;accept()?</p>
]]></description><link>https://forum.qt.io/post/248760</link><guid isPermaLink="true">https://forum.qt.io/post/248760</guid><dc:creator><![CDATA[lucadanieli]]></dc:creator><pubDate>Thu, 23 Oct 2014 11:09:48 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Thu, 23 Oct 2014 10:12:33 GMT]]></title><description><![CDATA[<p dir="auto">are you sure that the Key_Backtab key is not used or controlled by something else?</p>
]]></description><link>https://forum.qt.io/post/248752</link><guid isPermaLink="true">https://forum.qt.io/post/248752</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Thu, 23 Oct 2014 10:12:33 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Thu, 23 Oct 2014 09:57:05 GMT]]></title><description><![CDATA[<p dir="auto">Yep, but not perfectly. In my opinion only QComboBox::keyPressEvent(keyEvent) in the Backtab section doesn't work.</p>
<p dir="auto">My code is thought like this.<br />
I have a comboBox connected to an editor which contains a document. Changing the comboBox's index, the document changes. So, pressing Key_Tab to move forward:</p>
<p dir="auto">document_1 -&gt; document_2 -&gt; document_3<br />
hlight_row_1 -&gt; hlight_row_2 -&gt; hlight_row_3</p>
<p dir="auto">This works perfectly.<br />
Instead, if I press Key_Backtab (backward), the behaviour is:</p>
<p dir="auto">document_1 -&gt; document_3 -&gt; document_2 --------//which is right<br />
hlight_row_1 -&gt; hlight_row_1 -&gt; hlight_row_1 -------//stays still</p>
<p dir="auto">the code in the Backtab section, then, seems to be executed, because my documents in the editor are changing backwards, but just the highlighting of the respective rows inside the comboBox popup list doesn't.<br />
Deleting the QComboBox::keyPressEvent(keyEvent) line, I obtain the same exact behaviour. So I thought that for some reason keyPressEvent wasn't work.</p>
]]></description><link>https://forum.qt.io/post/248751</link><guid isPermaLink="true">https://forum.qt.io/post/248751</guid><dc:creator><![CDATA[lucadanieli]]></dc:creator><pubDate>Thu, 23 Oct 2014 09:57:05 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Thu, 23 Oct 2014 07:45:21 GMT]]></title><description><![CDATA[<p dir="auto">I didn't understand that phrase. Do you mean that your code is currently working ?</p>
]]></description><link>https://forum.qt.io/post/248746</link><guid isPermaLink="true">https://forum.qt.io/post/248746</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 23 Oct 2014 07:45:21 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Wed, 22 Oct 2014 21:32:45 GMT]]></title><description><![CDATA[<p dir="auto">I'll test it, but actually the "case statement" works, asf I can confirm that setCurrentIndex works.</p>
]]></description><link>https://forum.qt.io/post/248696</link><guid isPermaLink="true">https://forum.qt.io/post/248696</guid><dc:creator><![CDATA[lucadanieli]]></dc:creator><pubDate>Wed, 22 Oct 2014 21:32:45 GMT</pubDate></item><item><title><![CDATA[Reply to Why keyPressEvent doesn&#x27;t work? on Wed, 22 Oct 2014 20:48:00 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I wonder if you should not rather test for the shift modifier + Key_Tab (haven't tested it)</p>
<p dir="auto">Hope it helps</p>
]]></description><link>https://forum.qt.io/post/248687</link><guid isPermaLink="true">https://forum.qt.io/post/248687</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 22 Oct 2014 20:48:00 GMT</pubDate></item></channel></rss>