[Interest] How to create a list.

william.crocker at analog.com william.crocker at analog.com
Wed Jan 13 02:10:17 CET 2016


On 01/12/2016 07:37 PM, Elvis Stansvik wrote:
> 2016-01-12 15:37 GMT+01:00 Bill Crocker<william.crocker at analog.com>:
>> Hello:
>>
>> Could someone please show me the few lines of code required
>> to add a list *of three elements* to a QTextDocument using low
>> level (i.e QTextBlock, QTextCursor, QTextList, etc.) classes.
>
> This would be one way:
>

That is all good stuff.

I had solved my problem and was going to fess up in the next few hours.
The part I needed was that little itsy-bitsy, under documented function
named add() which is hidden in plain sight at the top of the QTextList
documentation.

In my own defense, it is not used in qtexthtmlparser.cpp or qtextedit.cpp
which were the fist two stops (Qt 4.8.6) I made looking for answers.

Also, the documentation (Qt 4.8.6) does not appear to show you how to add the
second item to a list which is not at all like adding the first.

My application is also a little different. I am parsing my own XML representation
of a document, and so would not parse the text and then make a second pass
to apply the structure. But I am good now that I know the add() trick.

Thanks.

Bill

> #include<QApplication>
> #include<QTextBlock>
> #include<QTextCursor>
> #include<QTextDocument>
> #include<QTextEdit>
> #include<QTextList>
> #include<QTextListFormat>
>
> int main(int argc, char *argv[]) {
>      QApplication app(argc, argv);
>
>      QTextEdit edit;
>      QTextDocument *doc = edit.document();
>
>      // Insert text.
>      QTextCursor cursor(doc);
>      cursor.insertText("A list of three items:\n");
>      cursor.insertText("One\n");
>      cursor.insertText("Two\n");
>      cursor.insertText("Three");
>
>      // Position cursor on "One".
>      cursor.movePosition(QTextCursor::Start);
>      cursor.movePosition(QTextCursor::NextBlock);
>
>      // Insert list.
>      QTextListFormat listFormat;
>      listFormat.setStyle(QTextListFormat::ListDisc);
>      QTextList *list = cursor.insertList(listFormat);
>
>      // Add "Two".
>      cursor.movePosition(QTextCursor::NextBlock);
>      list->add(cursor.block());
>
>      // Add "Three".
>      cursor.movePosition(QTextCursor::NextBlock);
>      list->add(cursor.block());
>
>      edit.show();
>
>      return app.exec();
> }
>
> Regards,
> Elvis
>
>>
>> Thanks.
>>
>> --
>> Bill
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>
>



More information about the Interest mailing list