[Interest] How to create a list.

william.crocker at analog.com william.crocker at analog.com
Wed Jan 13 12:16:03 CET 2016


On 01/13/2016 05:33 AM, Konstantin Tokarev wrote:
>
>
> 13.01.2016, 04:10, "william.crocker at analog.com"<william.crocker at analog.com>:
>> 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.
>
> Hi Bill,
>
> You may want to use DOM parser to make things more convenient.
>If you don't want to use QDom (there a many reasons not to use it), you cat try 
great PugiXML parser:

I parse XML and traverse with QDom. That is not the problem.
Figuring out how to create a QTextDocument from it for use by QTextEdit was the 
problem.
(IMHO QTextDocument has design and usability issues, but I would probably not 
have done any
better given the complexity of the problem domain.)

I could just use the native HTML read/write capability, but I need to add my own 
special
object types and it does not appear possible to save/restore them with HTML.

Is it possible save/restore my own objects using HTML?

I use the QTextObjectInterface interface to add them to QTextDocument, but did
not see how to save/restore. That is why I was forced to my own XML file format.

Thanks.

Bill

>
> http://pugixml.org/
>
>>
>> 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
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>



More information about the Interest mailing list