Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there anywhere an example how to create a QTextDocument containing a list?
Forum Updated to NodeBB v4.3 + New Features

Is there anywhere an example how to create a QTextDocument containing a list?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 6.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wurgl
    wrote on last edited by
    #1

    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.

    JKSHJ 1 Reply Last reply
    0
    • W Wurgl

      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.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      Hi @Wurgl,

      You can use HTML code to create lists. Something like:

      textDocument->setHtml("<ul><li>Item 1</li><li>Item 2</li></ul>");
      

      See also: http://doc.qt.io/qt-5/richtext-html-subset.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Wurgl
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Then QTextListFormat is the starting point

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • W Offline
            W Offline
            Wurgl
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Text Edit example

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved