<?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[KeyPressEvent]]></title><description><![CDATA[<p dir="auto">Hi guys<br />
So i have this code where i try to press delete from my keyboard and expect the program to catch this event and do something like a MessageBox or a qDebug.<br />
It doesn't give me error but doesn't work either.<br />
I don't understand what i'm doing wrong.</p>
<p dir="auto">Main Window.h</p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QTableWidget&gt;
#include &lt;QTableWidgetItem&gt;
#include &lt;QListWidgetItem&gt;
#include &lt;QKeyEvent&gt;
#include &lt;QMessageBox&gt;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

private slots:

void on_keyPressEvent(QKeyEvent *event);

private:
    Ui::MainWindow *ui;

};
#endif // MAINWINDOW_H

</code></pre>
<p dir="auto">Main Window .cpp</p>
<pre><code>void MainWindow::on_keyPressEvent(QKeyEvent *event){
    if(event-&gt;key() == Qt::Key_Delete){
       QMessageBox::information(this,"Hello","You Have pressed delete");
       qDebug()&lt;&lt;"do some code here ";
    }
</code></pre>
<p dir="auto">If anyone knows what's missing i would really appreciate some help, thanks.</p>
]]></description><link>https://forum.qt.io/topic/139872/keypressevent</link><generator>RSS for Node</generator><lastBuildDate>Sat, 12 Sep 2026 19:50:52 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139872.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 07 Oct 2022 12:48:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to KeyPressEvent on Fri, 07 Oct 2022 12:56:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/apprenticereno">@<bdi>ApprenticeReno</bdi></a> Key press event (as its name suggests) is an event, not a signal. So, you need to override the <a href="https://doc.qt.io/qt-6/qwidget.html#keyPressEvent" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qwidget.html#keyPressEvent</a> method in your subclass.</p>
<pre><code>class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

protected:   
    void keyPressEvent(QKeyEvent *event) override;

</code></pre>
<p dir="auto">See also <a href="https://doc.qt.io/qt-6/eventsandfilters.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/eventsandfilters.html</a></p>
]]></description><link>https://forum.qt.io/post/731718</link><guid isPermaLink="true">https://forum.qt.io/post/731718</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 07 Oct 2022 12:56:02 GMT</pubDate></item><item><title><![CDATA[Reply to KeyPressEvent on Fri, 07 Oct 2022 15:48:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/apprenticereno">@<bdi>ApprenticeReno</bdi></a><br />
Qt uses a convention that if a method name ends in <code>Event</code> it is a <code>virtual</code> method you can <code>override</code>, and not a signal/slot.  There has long been debate about which "happenings" should be "events" versus "signals", not for you to worry, just keep an eye out for that <code>Event</code> in the name :)</p>
]]></description><link>https://forum.qt.io/post/731757</link><guid isPermaLink="true">https://forum.qt.io/post/731757</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 07 Oct 2022 15:48:32 GMT</pubDate></item><item><title><![CDATA[Reply to KeyPressEvent on Fri, 07 Oct 2022 13:20:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a>  I thought there was no difference between a signal and an event, my bad.</p>
<pre><code>//mainwindow.h

protected:
void keyPressEvent(QKeyEvent *event) override;
</code></pre>
<p dir="auto">And</p>
<pre><code>//mainwindow.cpp

void MainWindow::keyPressEvent(QKeyEvent *event){

if(event-&gt;key() == Qt::Key_Delete){

QMessageBox::information(this,"hello","you pressed delete");
}

}

</code></pre>
<p dir="auto">Tried this way and it works. <a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> Thank you very much.</p>
]]></description><link>https://forum.qt.io/post/731725</link><guid isPermaLink="true">https://forum.qt.io/post/731725</guid><dc:creator><![CDATA[ApprenticeReno]]></dc:creator><pubDate>Fri, 07 Oct 2022 13:20:24 GMT</pubDate></item><item><title><![CDATA[Reply to KeyPressEvent on Fri, 07 Oct 2022 12:56:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/apprenticereno">@<bdi>ApprenticeReno</bdi></a> Key press event (as its name suggests) is an event, not a signal. So, you need to override the <a href="https://doc.qt.io/qt-6/qwidget.html#keyPressEvent" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qwidget.html#keyPressEvent</a> method in your subclass.</p>
<pre><code>class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

protected:   
    void keyPressEvent(QKeyEvent *event) override;

</code></pre>
<p dir="auto">See also <a href="https://doc.qt.io/qt-6/eventsandfilters.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/eventsandfilters.html</a></p>
]]></description><link>https://forum.qt.io/post/731718</link><guid isPermaLink="true">https://forum.qt.io/post/731718</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 07 Oct 2022 12:56:02 GMT</pubDate></item></channel></rss>