PDA

View Full Version : How to change current line format in QTextEdit?



FinderCheng
24th March 2011, 15:49
Hi, there! I want to find out how to change current line format in QTextEdit?

In the document I read that formatting can be applied to the current text document using the setCharFormat(), mergeCharFormat(), setBlockFormat() and mergeBlockFormat() functions. If the cursor has no selection, current block format will be changed. But in my application, the current block in which cursor is couldn't be changed. May I missed something? Then how could I change current block format which has no selection?

Thank you all!

wysota
24th March 2011, 15:58
What have you tried so far?

FinderCheng
25th March 2011, 01:04
Here is what did:


QTextCursor cursor = this->textCursor();
QTextBlockFormat blockFmt;
blockFmt.setNonBreakableLines(true);
blockFmt.setPageBreakPolicy(QTextFormat::PageBreak _AlwaysBefore);
QTextCharFormat charFmt;
charFmt.setFont(data->visualFont());
if(!cursor.hasSelection()) {
cursor.beginEditBlock();
cursor.setBlockFormat(blockFmt);
cursor.mergeBlockCharFormat(charFmt);
QTextBlock block = cursor.block();
block.setUserData(data);
cursor.endEditBlock();
}


What I want to do is: change current line's format if there is no selection. So if cursor.hasSelection() is false, I just merge new format to block chars. But this does not work.

wysota
25th March 2011, 01:32
You have to set the cursor back on the text edit.

FinderCheng
25th March 2011, 02:11
I added this->setTextCorsor(cursor); after cursor.endEditBlock();, but it still doesn't work. In fact, after adding this, the whole block becomes invisible.

wysota
25th March 2011, 10:41
What is the ultimate result you want to receive? Do you wish to change the format of the whole block or what exactly?