Search code examples
c++qttextedit

Trying to clear a textEdit box in QT C++ (using QT Creator)


I'm new to QT, young at programming, and not understanding all the help materials for the classes in QT.

I have the following code, which executes on a button click, inserting text. This works fine but I want to clear the textEdit first, then insert the new text. Can anyone point me in the right direction here? Any help is much appreciated.

QTextCharFormat textFormat;
QTextCursor cursor(ui->textEdit->textCursor());
cursor.insertText("<some text to insert", textFormat);

It appears that I have to use setPosition() and movePosition() to select the text, then removeSelectedText() to clear it. I don't know how to determine the first and last positions in the document.


Solution

  • QTextEdit has a clear function, which deletes all text. In the documentation it's listed as a Slot.

    QTextEdit::clear()