Is there anywhere an example how to create a QTextDocument containing a list?
-
It seems that I am not smart enough to understand how theses QText*-Classes work. I would like to create something like:
- item 1
some description - item 2
…
I would like to create this from some data and store it as html (or whatever format) without using a gui. Somehow I miss the basics and I miss some example.
- item 1
-
Hi @Wurgl,
You can use HTML code to create lists. Something like:
textDocument->setHtml("<ul><li>Item 1</li><li>Item 2</li></ul>");
-
Writing HTML-Code to create a a QTextDocument with lists which later shall be used to create HTML code … Sound like there is a big potential for optimization.
I would rather have a chance to understand how these group of classes work.
-
Hi,
Then QTextListFormat is the starting point
-
That's what I had before I started to ask.
#include <QtWidgets/qapplication.h> #include <QtGui/qtextdocumentwriter.h> #include <QtGui/qtextdocument.h> #include <QtGui/qtextcursor.h> int main(int argc, char **argv) { QApplication app(argc, argv); QStringList listItems; listItems << "Item 1" << "Item 2" << "Item 3"; QStringList listDescs; listDescs << "Description 1" << "Description 2" << "Description 3"; QTextDocument page; page.setMetaInformation(QTextDocument::DocumentTitle, "The Title"); QTextCursor cursor(&page); cursor.createList(QTextListFormat::ListDisc); for(int i = 0; i < listItems.count(); ++ i) { cursor.insertText(listItems[i]); cursor.insertBlock(); } QTextDocumentWriter mainPage("./index.html", "HTML"); mainPage.write(&page); exit(0); } // :!g++ -o xtest -fPIC -Wall -W -Wextra -isystem /usr/include/qt5 % -L/usr/lib64 -lQt5Gui -lQt5Widgets -lQt5Core
Now I tried to add those data from 'listDescs' to the list without getting a bullet before every such line.
for(int i = 0; i < listItems.count(); ++ i) { QTextDocument listElement; QTextCursor elementCursor(&listElement); cursor.insertText(listItems[i]); cursor.insertText("\n"); cursor.insertText(listDescs[i]); cursor.insertFragment(QTextDocumentFragment(&listElement)); cursor.insertBlock(); }
This did not work, it just doubled the bullets.
Okay. So do it different. Bottom-up instead of top-down.
for(int i = 0; i < listItems.count(); ++ i) { QTextDocument listElement; QTextCursor elementCursor(&listElement); elementCursor.insertText(listItems[i]); QTextFrameFormat frame; frame.setBorderStyle(QTextFrameFormat::BorderStyle_None); elementCursor.insertFrame(frame); elementCursor.insertText(listDescs[i]); cursor.insertFragment(QTextDocumentFragment(&listElement)); cursor.insertBlock(); }
Oops! I do not even see a <ul>-Element in the created html. Where is my list?
And therefore I asked for some example.
-