Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Clearing a QTextWidget including its internal state
QtWS25 Last Chance

Clearing a QTextWidget including its internal state

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 429 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wurgl
    wrote on last edited by
    #1

    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;
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      It's not really in a link, it's just blue underlined. You need to reset the format. Add edit->setCurrentCharFormat(QTextCharFormat()); before edit->setPlainText("I am plain text");

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      W 1 Reply Last reply
      3
      • VRoninV VRonin

        It's not really in a link, it's just blue underlined. You need to reset the format. Add edit->setCurrentCharFormat(QTextCharFormat()); before edit->setPlainText("I am plain text");

        W Offline
        W Offline
        Wurgl
        wrote on last edited by
        #3

        @VRonin Thanks! That solved it.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved