Clearing a QTextWidget including its internal state
Solved
General and Desktop
-
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.
To see what I mean do the following steps:
- open in some browser (i use Firefox) https://en.wikipedia.org/wiki/Test
- copy the incomplete line "To make test edits on Wikipedia, please use the san"
==> Important here: stop the selection in the middle of the link! - paste that line into the QTextWidget (you can also use drag & drop)
- click on the button "Clear"
==> the widget still thinks it is in the middle of a link.
How can I reset this?
I tried to set a new QTextDocument, but this dows not clear too.
I tried to turn of acceptRichText before setting the new plain text and turning it on afterwards, did not help.Any idea?
Attached is a minimal example (compile in gcc with -std=c++11).
#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->setAcceptDrops(true); QToolButton *button = new QToolButton(this); button->setText("Clear"); l->addWidget(edit); l->addWidget(button); connect(button, &QToolButton::clicked, [this] (bool checked) { edit->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; }
-
It's not really in a link, it's just blue underlined. You need to reset the format. Add
edit->setCurrentCharFormat(QTextCharFormat());
beforeedit->setPlainText("I am plain text");