<?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[TextEdit and eventFilter: characters not appearing on ui element]]></title><description><![CDATA[<p dir="auto">Hi everyone.</p>
<p dir="auto">I have a <code>textEdit</code> widget on my <code>mainwindow.ui</code>, and I would like to catch the key pressed by the user when the focus is on the <code>textEdit</code>.<br />
I gave a read <a href="https://doc.qt.io/qt-5/eventsandfilters.html#event-filters" target="_blank" rel="noopener noreferrer nofollow ugc">here</a> and adapted the example to this:</p>
<p dir="auto"><code>mainwindow.h</code></p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

protected:
    bool eventFilter(QObject *obj, QEvent *ev) override;
};

#endif // MAINWINDOW_H
</code></pre>
<p dir="auto"><code>mainwindow.cpp</code></p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    ui-&gt;textEdit-&gt;setFocus();
    ui-&gt;textEdit-&gt;installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
//    if (obj == textEdit) {
        if (event-&gt;type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast&lt;QKeyEvent*&gt;(event);
            qDebug() &lt;&lt; "Ate key press " &lt;&lt; keyEvent-&gt;text();
            return true;
        } else {
            return false;
        }
//    } else {
//        // pass the event on to the parent class
//        return QMainWindow::eventFilter(obj, event);
//    }
}
</code></pre>
<p dir="auto"><code>mainwindow.ui</code></p>
<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;ui version="4.0"&gt;
 &lt;class&gt;MainWindow&lt;/class&gt;
 &lt;widget class="QMainWindow" name="MainWindow"&gt;
  &lt;property name="geometry"&gt;
   &lt;rect&gt;
    &lt;x&gt;0&lt;/x&gt;
    &lt;y&gt;0&lt;/y&gt;
    &lt;width&gt;400&lt;/width&gt;
    &lt;height&gt;300&lt;/height&gt;
   &lt;/rect&gt;
  &lt;/property&gt;
  &lt;property name="windowTitle"&gt;
   &lt;string&gt;MainWindow&lt;/string&gt;
  &lt;/property&gt;
  &lt;widget class="QWidget" name="centralWidget"&gt;
   &lt;widget class="QTextEdit" name="textEdit"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;100&lt;/x&gt;
      &lt;y&gt;50&lt;/y&gt;
      &lt;width&gt;221&lt;/width&gt;
      &lt;height&gt;141&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
  &lt;/widget&gt;
  &lt;widget class="QMenuBar" name="menuBar"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;0&lt;/x&gt;
     &lt;y&gt;0&lt;/y&gt;
     &lt;width&gt;400&lt;/width&gt;
     &lt;height&gt;25&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
  &lt;/widget&gt;
  &lt;widget class="QToolBar" name="mainToolBar"&gt;
   &lt;attribute name="toolBarArea"&gt;
    &lt;enum&gt;TopToolBarArea&lt;/enum&gt;
   &lt;/attribute&gt;
   &lt;attribute name="toolBarBreak"&gt;
    &lt;bool&gt;false&lt;/bool&gt;
   &lt;/attribute&gt;
  &lt;/widget&gt;
  &lt;widget class="QStatusBar" name="statusBar"/&gt;
 &lt;/widget&gt;
 &lt;layoutdefault spacing="6" margin="11"/&gt;
 &lt;resources/&gt;
 &lt;connections/&gt;
&lt;/ui&gt;
</code></pre>
<p dir="auto">The keys pressed are shown correctly on the output, but they do not appear on my <code>textEdit</code>.</p>
<p dir="auto">How can I solve this?</p>
]]></description><link>https://forum.qt.io/topic/119779/textedit-and-eventfilter-characters-not-appearing-on-ui-element</link><generator>RSS for Node</generator><lastBuildDate>Wed, 18 Nov 2026 14:41:37 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/119779.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 08 Oct 2020 18:05:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to TextEdit and eventFilter: characters not appearing on ui element on Thu, 08 Oct 2020 18:23:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a><br />
Hi. Ok, I changed <code>eventFilter()</code> a little bit:</p>
<pre><code>bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui-&gt;textEdit) {
        if (event-&gt;type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast&lt;QKeyEvent*&gt;(event);
            qDebug() &lt;&lt; "Ate key press " &lt;&lt; keyEvent-&gt;text();
        }
    }
    return QMainWindow::eventFilter(obj, event);
}
</code></pre>
<p dir="auto">and now it works. Thanks!</p>
]]></description><link>https://forum.qt.io/post/621397</link><guid isPermaLink="true">https://forum.qt.io/post/621397</guid><dc:creator><![CDATA[Tamfub]]></dc:creator><pubDate>Thu, 08 Oct 2020 18:23:43 GMT</pubDate></item><item><title><![CDATA[Reply to TextEdit and eventFilter: characters not appearing on ui element on Thu, 08 Oct 2020 18:09:14 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Because you are stoping the event processing there by returning true exactly how it is explained in the documentation you are linking.</p>
]]></description><link>https://forum.qt.io/post/621393</link><guid isPermaLink="true">https://forum.qt.io/post/621393</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 08 Oct 2020 18:09:14 GMT</pubDate></item></channel></rss>