I\'m completely new to C++ and Qt.
I want to populate a QTextEdit
object with QTextBlocks
, how do I do that?
e.g. If I hav
QTextEdit
will let you add your contents via a QString
:
QTextEdit myEdit("the fish are coming");
It also allows you to use a QTextDocument
, which holds blocks of text.
The QTextDocument
itself also can accept a QString
:
QTextEdit myEdit;
QTextDocument* myDocument = new QTextDocument("the fish are coming", &myEdit);
myEdit.setDocument(myDocument);
However, "If you need to create a new text block, or modify the contents of a document while examining its contents, use the cursor-based interface provided by QTextCursor
instead." (Qt documentation) (Note, I added the QTextBlockFormat
lines to make it explicit where the blocks are.)
QTextEdit myEdit;
QTextDocument* myDocument = new QTextDocument(&myEdit);
myEdit.setDocument(myDocument);
QTextCursor* myCursor = new QTextCursor(myDocument);
QTextBlockFormat format;
format.setBackground(Qt::red);
myCursor->setBlockFormat(format);
myCursor->insertText("the ");
format.setBackground(Qt::green);
myCursor->insertBlock(format);
myCursor->insertText("fish ");
format.setBackground(Qt::yellow);
myCursor->insertBlock(format);
myCursor->insertText("are ");
format.setBackground(Qt::red);
myCursor->insertBlock(format);
myCursor->insertText("coming!");
format.setBackground(Qt::green);
myCursor->insertBlock(format);
myCursor->insertText(QString(%1 blocks").arg(myDocument->blockCount()));
myEdit.show();
Seems like a lot of effort to go through to me. Can you give any additional information on why you feel you need to use QTextBlock
s?