<?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[Reset the editor of a QTableWidget instead of entering it when the editor loses focus]]></title><description><![CDATA[<p dir="auto">Hi :-)</p>
<p dir="auto">I have a <code>QTableWidgetItem</code> I use a <code>QStyledItemDelegate</code> on (mostly because I needed a custom <code>setModelData</code> function).</p>
<p dir="auto">I would like to change the default behavior of editing, which is: The editing is finished if the user presses Enter, Return or the editor loses focus (which seems to be the default for a <code>QLineEdit</code>, according to the docs. At least, in this case, the <code>editingFinished</code> signal is emitted). I don't want the data to be comitted on focus loss, only on Enter/Return pressing.</p>
<p dir="auto">So I created my own <code>QLineEdit</code> derived editor and implemented a custom <code>createEditor</code> function in the delegate, using this one.</p>
<p dir="auto">Ifirst tried to reimplement the editor's <code>focusOutEvent</code>, where I set the entered text back to the initial value before passing the event to the base class. Problem is that <code>setModelData</code> is called <em>before</em> the <code>focusOutEvent</code> function.</p>
<p dir="auto">I then tried to reimplement the whole <code>event</code> function and looked for something to happen earlier. The first thing I found was <code>event-&gt;type()</code> to be <code>QEvent::aboutToChangeFocus</code>, but even with this one, <code>setModelData</code> is called earlier.</p>
<p dir="auto">I also tried to figure out how the editor is connected to the delegate and at which very point <code>setModelData</code> is invoked. But I didn't find it (those Qt sources are quite complicated sometimes :-( at least for a hobby programmer like I am).</p>
<p dir="auto">So is there any way to change this? One can break this down into the question: How can I know if the editor was closed via pressing Enter/Return or via focus loss?</p>
<p dir="auto">Thanks for all help!</p>
]]></description><link>https://forum.qt.io/topic/104935/reset-the-editor-of-a-qtablewidget-instead-of-entering-it-when-the-editor-loses-focus</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 14:38:03 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/104935.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 14 Jul 2019 06:09:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 19:42:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> The table widget I use is not intended to be used like a spread sheet (LibreOffice or Excel). It's a list of names with additional columns one can trigger actions on. And normally, one doesn't edit the name, as long as one doesn't see a typo or such in it.</p>
<p dir="auto">So clicking outside the edited name item is either a mistake or a "oh, I wanted to do something else before". So I think, for my use-case, it's better to have the user to confirm an edit action by pressing Enter or Return rather than clicking somewhere outside the editor.</p>
<p dir="auto">Maybe a corner-case ;-)</p>
]]></description><link>https://forum.qt.io/post/540755</link><guid isPermaLink="true">https://forum.qt.io/post/540755</guid><dc:creator><![CDATA[l3u_]]></dc:creator><pubDate>Sun, 14 Jul 2019 19:42:01 GMT</pubDate></item><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 18:50:23 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Out of curiosity, why do you want that behavior ?</p>
<p dir="auto">It's kind of counter intuitive with regard to how applications work with that kind of editing feature.</p>
]]></description><link>https://forum.qt.io/post/540750</link><guid isPermaLink="true">https://forum.qt.io/post/540750</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sun, 14 Jul 2019 18:50:23 GMT</pubDate></item><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 19:41:01 GMT]]></title><description><![CDATA[<p dir="auto">Okay. I worked around it. I implemented a custom <code>QLineEdit</code> used by the delegate with:</p>
<pre><code>void PlayersEditor::keyPressEvent(QKeyEvent *event)
{
    if (event-&gt;key() == Qt::Key_Return || event-&gt;key() == Qt::Key_Enter) {
        m_enterPressed = true;
    }
    QLineEdit::keyPressEvent(event);
}
</code></pre>
<p dir="auto">and</p>
<pre><code>bool PlayersEditor::enterPressed() const
{
    return m_enterPressed;
}
</code></pre>
<p dir="auto">And in my <code>QStyledItemDelegate</code>'s <code>setModelData</code>, I simply do</p>
<pre><code>if (! m_editor-&gt;enterPressed()) {
    return;
}
</code></pre>
<p dir="auto">Which leaves the model unchanged and "just" closes the editor. Maybe not the "right" way, but it works …</p>
<p dir="auto">Edit: Just to mention this: one has to declare m_editor as mutable to be able tol set it inside <code>createEditor</code>, as this function is const.</p>
]]></description><link>https://forum.qt.io/post/540700</link><guid isPermaLink="true">https://forum.qt.io/post/540700</guid><dc:creator><![CDATA[l3u_]]></dc:creator><pubDate>Sun, 14 Jul 2019 19:41:01 GMT</pubDate></item><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 09:13:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> If Enter/Return is pressed in the editor, the data should be comitted. Just as it's the case now. But if the editor loses focus, nothing should happen, the editing should be aborted. Per default, if I click somewhere outside the editor (after having changed something), it's the same as if I pressed Enter/Return.</p>
<p dir="auto">The problem is that I don't know what to change to achieve this and where to change it (in a custom editor or the delegate) … also, I don't know at which point which signals from the editor are connected to the delegate.</p>
<p dir="auto">What I don't understand is: If the <code>focusOutEvent</code> causes <code>editingFinshed</code> to be emitted which finally causes <code>commitData</code> to be emitted and <code>setModelData</code> to be called, then why is <code>setModelData</code> called <em>before</em> I see <code>focusOutEvent</code> being called?!</p>
]]></description><link>https://forum.qt.io/post/540695</link><guid isPermaLink="true">https://forum.qt.io/post/540695</guid><dc:creator><![CDATA[l3u_]]></dc:creator><pubDate>Sun, 14 Jul 2019 09:13:19 GMT</pubDate></item><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 08:12:11 GMT]]></title><description><![CDATA[<p dir="auto">Seems like I still don't understand what you want to achieve - you <em>don't</em> want to commit the data when the focus changes, or why do you try to fiddle around with the focus event?<br />
You want to commit the data when enter is pressed - then you have to watch for Qt::KeyEnter in the QLineEdit::keyPressEvent() and then emit QAbstractItemDelegate::commitData() - this is what's done for the focusOut/ChangeEvent as you can see in the backtrace.</p>
]]></description><link>https://forum.qt.io/post/540694</link><guid isPermaLink="true">https://forum.qt.io/post/540694</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sun, 14 Jul 2019 08:12:11 GMT</pubDate></item><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 08:02:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> Apparently, I just can't read it?!</p>
<pre><code>#0  0x00005555556783de in PlayersPageDelegate::setModelData(QWidget*, QAbstractItemModel*, QModelIndex const&amp;) const ()
#1  0x00007ffff7a8f325 in QAbstractItemView::commitData(QWidget*) ()
from /usr/lib64/libQt5Widgets.so.5
#2  0x00007ffff7a8e480 in ?? () from /usr/lib64/libQt5Widgets.so.5
#3  0x00007ffff6f5daae in QMetaObject::activate(QObject*, int, int, void**) ()
from /usr/lib64/libQt5Core.so.5
#4  0x00007ffff7aa8adf in QAbstractItemDelegate::commitData(QWidget*) ()
from /usr/lib64/libQt5Widgets.so.5
#5  0x00007ffff7aa9845 in ?? () from /usr/lib64/libQt5Widgets.so.5
#6  0x00007ffff7aa99a8 in ?? () from /usr/lib64/libQt5Widgets.so.5
#7  0x00007ffff6f5e66a in QObject::event(QEvent*) () from /usr/lib64/libQt5Core.so.5
#8  0x00007ffff7841501 in QApplicationPrivate::notify_helper(QObject*, QEvent*) ()
from /usr/lib64/libQt5Widgets.so.5
#9  0x00007ffff7848af8 in QApplication::notify(QObject*, QEvent*) ()
from /usr/lib64/libQt5Widgets.so.5
#10 0x00007ffff6f358a1 in QCoreApplication::notifyInternal2(QObject*, QEvent*) ()
from /usr/lib64/libQt5Core.so.5
#11 0x00007ffff6f389c6 in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) ()
from /usr/lib64/libQt5Core.so.5
#12 0x00007ffff6f86763 in ?? () from /usr/lib64/libQt5Core.so.5
#13 0x00007ffff5b046fd in g_main_context_dispatch () from /usr/lib64/libglib-2.0.so.0
#14 0x00007ffff5b04998 in ?? () from /usr/lib64/libglib-2.0.so.0
#15 0x00007ffff5b04a2c in g_main_context_iteration () from /usr/lib64/libglib-2.0.so.0
#16 0x00007ffff6f86503 in QEventDispatcherGlib::processEvents(QFlags&lt;QEventLoop::ProcessEventsFlag&gt;)
    () from /usr/lib64/libQt5Core.so.5
#17 0x00007ffff6f34873 in QEventLoop::exec(QFlags&lt;QEventLoop::ProcessEventsFlag&gt;) ()
from /usr/lib64/libQt5Core.so.5
#18 0x00007ffff6f3c482 in QCoreApplication::exec() () from /usr/lib64/libQt5Core.so.5
#19 0x00005555556034c6 in main ()
</code></pre>
<p dir="auto">Seems like it's called by <code>QAbstractItemDelegate::commitData</code>, but I can't override it in my delegate ("… is marked as override but doesn't")? [Edit:] Okay, that's because it's a signal, not a function.</p>
]]></description><link>https://forum.qt.io/post/540692</link><guid isPermaLink="true">https://forum.qt.io/post/540692</guid><dc:creator><![CDATA[l3u_]]></dc:creator><pubDate>Sun, 14 Jul 2019 08:02:41 GMT</pubDate></item><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 07:48:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/l3u_">@<bdi>l3u_</bdi></a> said in <a href="/post/540689">Reset the editor of a QTableWidget instead of entering it when the editor loses focus</a>:</p>
<blockquote>
<p dir="auto">but I still don't know how it works …</p>
</blockquote>
<p dir="auto">You should not know how it works you should take a look at the backtrace to see from where it is triggered - somewhere the text must be read from the QLineEdit which is nearby the place you can jump in.</p>
]]></description><link>https://forum.qt.io/post/540691</link><guid isPermaLink="true">https://forum.qt.io/post/540691</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sun, 14 Jul 2019 07:48:57 GMT</pubDate></item><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 07:41:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> I'm not sure I'm fit enough with this stuff but I tried it ;-)</p>
<p dir="auto">Apparently, <code>setModelData</code> is called via <code>QAbstractItemDelegate::commitData(QWidget*)</code>, but I still don't know how it works …</p>
<p dir="auto">Question is if I need to know how the internals work, as I only want to handle the <code>editingFinished</code> signal (I suppose) and decide, based on if Enter/Return has been pressed or the focus was lost, if I reset or commit the data …</p>
]]></description><link>https://forum.qt.io/post/540689</link><guid isPermaLink="true">https://forum.qt.io/post/540689</guid><dc:creator><![CDATA[l3u_]]></dc:creator><pubDate>Sun, 14 Jul 2019 07:41:50 GMT</pubDate></item><item><title><![CDATA[Reply to Reset the editor of a QTableWidget instead of entering it when the editor loses focus on Sun, 14 Jul 2019 06:59:01 GMT]]></title><description><![CDATA[<p dir="auto">Set a breakpoint in setModelData() and take a look at the backtrace to see from where it is called.</p>
]]></description><link>https://forum.qt.io/post/540685</link><guid isPermaLink="true">https://forum.qt.io/post/540685</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sun, 14 Jul 2019 06:59:01 GMT</pubDate></item></channel></rss>