How do I use QTextBlock?
Asked Answered
H

3

9

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 have the sentence "the fish are coming" how would I put each word into its own QTextBlock and add that block to QTextEdit, or have I misunderstood how QTextBlock actually works?

Hawsepiece answered 4/12, 2009 at 20:46 Comment(0)
G
14

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 QTextBlocks?

Gibeon answered 4/12, 2009 at 22:24 Comment(2)
I'm writing a program to help people to read another language (Greek) and each word has different grammatical information (such as "Verb, Indicative, Active, 3rd Person, Singular") which I would like to display when the mouse is moved over each word.Hawsepiece
Ah, that makes more sense then. One problem with my example is that each block is displayed as its own line. I don't have enough experience with QTextBlocks to know how to get them on the same line. Good luck!Gibeon
S
2

Keep them on the same line by using insertText without using insertBlock between.

For instance, when I tried

cursor.insertText("I will try ", textFormat);
cursor.insertText("this for you.", textFormat);

for you, the words all appeared on the same line.

insertBlock inserts a paragraph.

Stelle answered 26/2, 2013 at 5:17 Comment(1)
Is there a way to do this with multiple QTextBlocks ? As I understand it, this creates multiple fragments in one QTextBlock.Homeopathic
A
0

You should check the documentation here

You could assign your string to a QString and then add that to the QTextEdit, or you could parse the QString using section() see here

Armchair answered 4/12, 2009 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.