PDA

View Full Version : How to remove QTextListFormat text style?



Fred
20th February 2010, 13:13
Hi,

I want to write a small text edit application, where the user can format text. But I have a problem with QTextListFormat. Its no problem to create bullets with QTextListFormat::setStyle but I have no idea how to remove this formation.

You can see this problem even in the official Qt textedit demo. Start the demo, select text, select for example the "Bullet List (Square)" format and the text will be formatted with bullets. Select "Standard" and nothing happens.
Is it possible to remove this formation?

Greetings,
Fred

alex.magalhaes
28th November 2012, 17:51
Yes, it's perfectly possible. The question was posted years ago, but I'm replying for future references.

To remove a list from a text block, you must call the list's remove() function, passing the block as a parameter. The example below shows how you could do that:


//let textEdit be your QTextEdit object. get its text cursor
QTextCursor cursor = textEdit->textCursor();

//get the cursor's current list, which you want to remove from the text.
QTextList* currentList = cursor.currentList();

//get the current block, which you want to remove from the current list
QTextBlock currentBlock = cursor.block();

//call the list's remove() function, passing the block as a parameter
currentList->remove(currentBlock);

//the list is now removed, but you still have to remove the list identation
QTextBlockFormat blockFormat = cursor.blockFormat();
blockFormat.setIndent(0);
cursor.setBlockFormat(blockFormat);

jjjjjjj
23rd February 2018, 16:20
In the official Qt textedit demo you have code like this:

QTextBlockFormat bfmt = QTextBlockFormat();
bfmt.setObjectIndex(-1);
cursor.mergeBlockFormat(bfmt);

And that doesent work. But if you set ObjectIndex to zero like this:
bfmt.setObjectIndex(0);

that works.