<?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[Clearing a QTextWidget including its internal state]]></title><description><![CDATA[<p dir="auto">When I select some text from the browser which ends in the middle of a link into a QTextWidget and afterwards fill new text with setPlainText() into the QTextWidget, the field still thinks that it is inside a link.</p>
<p dir="auto">To see what I mean do the following steps:</p>
<ul>
<li>open in some browser (i use Firefox) <a href="https://en.wikipedia.org/wiki/Test" target="_blank" rel="noopener noreferrer nofollow ugc">https://en.wikipedia.org/wiki/Test</a></li>
<li>copy the incomplete line "To make test edits on Wikipedia, please use the san"<br />
==&gt; Important here: stop the selection in the middle of the link!</li>
<li>paste that line into the QTextWidget (you can also use drag &amp; drop)</li>
<li>click on the button "Clear"<br />
==&gt; the widget still thinks it is in the middle of a link.</li>
</ul>
<p dir="auto">How can I reset this?</p>
<p dir="auto">I tried to set a new QTextDocument, but this dows not clear too.<br />
I tried to turn of acceptRichText before setting the new plain text and turning it on afterwards, did not help.</p>
<p dir="auto">Any idea?</p>
<p dir="auto">Attached is a minimal example (compile in gcc with -std=c++11).</p>
<pre><code>#include "QtCore/qdebug.h"
#include "QtWidgets/qapplication.h"
#include "QtWidgets/qwidget.h"
#include "QtWidgets/qboxlayout.h"
#include "QtWidgets/qtextedit.h"
#include "QtWidgets/qtoolbutton.h"

class Widget : public QWidget {
public:
  Widget() {
    QHBoxLayout *l = new QHBoxLayout(this);
    edit = new QTextEdit(this);
    edit-&gt;setAcceptDrops(true);
    QToolButton *button = new QToolButton(this);
    button-&gt;setText("Clear");
    l-&gt;addWidget(edit);
    l-&gt;addWidget(button);
    connect(button, &amp;QToolButton::clicked, [this] (bool checked) { edit-&gt;setPlainText("I am plain text"); });
  }
  QTextEdit *edit;
};

int main(int argc, char **argv) {
  QApplication app(argc, argv);
  Widget w;
  w.show();
  app.exec();
  return 0;
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/92387/clearing-a-qtextwidget-including-its-internal-state</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 22:58:38 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/92387.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 08 Jul 2018 14:02:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Clearing a QTextWidget including its internal state on Sun, 08 Jul 2018 15:12:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> Thanks! That solved it.</p>
]]></description><link>https://forum.qt.io/post/468152</link><guid isPermaLink="true">https://forum.qt.io/post/468152</guid><dc:creator><![CDATA[Wurgl]]></dc:creator><pubDate>Sun, 08 Jul 2018 15:12:14 GMT</pubDate></item><item><title><![CDATA[Reply to Clearing a QTextWidget including its internal state on Sun, 08 Jul 2018 14:16:47 GMT]]></title><description><![CDATA[<p dir="auto">It's not really in a link, it's just blue underlined. You need to reset the format. Add <code>edit-&gt;setCurrentCharFormat(QTextCharFormat());</code> before <code>edit-&gt;setPlainText("I am plain text");</code></p>
]]></description><link>https://forum.qt.io/post/468145</link><guid isPermaLink="true">https://forum.qt.io/post/468145</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Sun, 08 Jul 2018 14:16:47 GMT</pubDate></item></channel></rss>