[solved] Nested List support in QTextEdit
-
Hi, I'm trying to create the nested list using QTextEdit.
I tried with the below code snippet, but I'm getting the same number for all the blocks.
@QTextListFormat listFormat;
listFormat.setIndent(indent);
listFormat.setStyle(QTextListFormat::ListDecimal);const vector<int> cursorPositions = iter->getPositions(); vector<int>::const_iterator posIter = cursorPositions .begin(); vector<int>::const_iterator posIterEnd = cursorPositions .end(); cursor.beginEditBlock(); for(; posIter!= posIterEnd; posIter++) { cursor.setPosition(*posIter); cursor.insertList(listFormat); } cursor.endEditBlock()@
What is wrong in the above code snippet?
I already came across the below post which is about 2 years old.
http://qt-project.org/forums/viewthread/10062Can someone provide me the input?
-
Hi and welcome to devnet,
Please enclose your code with coding tags (one @ at the beginning and one at the end) it will make it readable.
I would also suggest to give a more complete example (so others may test it) and explaining what you are expecting and what you get
-
Hi SGaist, thanks for your reply.
I want to represent the nested list items like in the below example.
@- first item
- second Item
1. first nested item
2. second nested item - third item
@
I actually achieved it by using the below code snippet.
@int indentArray[2] = {1,2};
int positions[2][];
//just for example, may not compile
//calculated cursor positions for the above mentioned examples
positions[0] = {0, 11, 60};
positions[1] = {23, 41};
for(int i=0; i< 2; i++)
{
int indent = indentArray[i];
QTextListFormat listFormat;
listFormat.setIndent(indent);
listFormat.setStyle(QTextListFormat::ListDecimal);
QTextList * list = NULL;
{
bool isListFormatApplied = false;
cursor.beginEditBlock();
int size = positions[i].getSize(); //for example
for(int j=0; j < sizs; j++)
{
cursor.setPosition(positions[i][j]);
if(isListFormatApplied == false)
{
list = cursor.createList(listFormat);
isListFormatApplied = true;
}
else
{
QTextBlock block = cursor.block();
if(list)
{
list->add(block);
}
}
}
cursor.endEditBlock();
}
}@Please let me know if this is fine or if there is any other better way of getting this done.
-
Since you know your various list you could do something like
@
QTextEdit edit;
QTextCursor cursor(edit.document());
QTextListFormat listFormat;listFormat.setStyle(QTextListFormat::ListDecimal); listFormat.setNumberPrefix("("); listFormat.setNumberSuffix(")"); cursor.insertList(listFormat); cursor.insertText("test1\n"); cursor.insertText("test2\n"); cursor.insertText("test3\n"); cursor.insertText("test4"); cursor = edit.document()->find("test2"); cursor.movePosition(QTextCursor::EndOfLine); listFormat.setIndent(2); cursor.insertList(listFormat); cursor.insertText("indent test1\n"); cursor.insertText("indent test2"); edit.show();
@
Hope it helps