tags:

views:

68

answers:

2

is there a way to change the background color of a QTextBlock in a QTextDocument without using a subclass of QAbstractTextDocumentLayout. I have tried many ways and the effects are null. I am trying from the textCursor() method of a QPlainTextEditor and it seems practically everything is const.

+2  A: 

Hey,

Could this example help you ?

http://qt.nokia.com/doc/4.6/demos-textedit.html

You can find it also in QtDemos, on Demonstrations->Text Edit.

Hope it helps!

Andy M
Thanks, my bad I was only looking inside the examples and not the demos, I wonder why they are not examples.
yan bellavance
I wonder though if I will be able to do this programatically since the textedit only seems to modify the layout of a user selection. I will see as I go through the code :D
yan bellavance
+1  A: 

You could try the merge methods:

QTextCursor cur = edit->textCursor();
QTextCharFormat fmt;
fmt.setBackground(QBrush(Qt::gray));
cur.mergeBlockCharFormat(fmt);
yeah i tried something similar yesterday: blkFormat= new QTextBlockFormat(); blkFormat->setBackground(QBrush(Qt::green,Qt::SolidPattern)); blkFormat->setForeground(QBrush(Qt::black,Qt::SolidPattern));textCursor().setBlockFormat(*blkFormat);So when I saw you using mergeBlockCharFormat() I figured that is what I was missing but I end up with the same result which is the program gets interrupted by the debugger:
yan bellavance
i saw you using pointers to objects you create on heap (block formats) - there's really no need to do that (and hopefully you're cleaning them up after corresponding setBlockFormat()!) - just a warning
otherwise, can you post more code around the parts you're having trouble with? you need the text cursor point to the block (or a selection) you're modifying formats for.