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. [solved] Nested List support in QTextEdit
QtWS25 Last Chance

[solved] Nested List support in QTextEdit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.9k Views
  • 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.
  • P Offline
    P Offline
    pranap
    wrote on last edited by
    #1

    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/10062

    Can someone provide me the input?

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

      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

      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
      • P Offline
        P Offline
        pranap
        wrote on last edited by
        #3

        Hi SGaist, thanks for your reply.

        I want to represent the nested list items like in the below example.
        @

        1. first item
        2. second Item
          1. first nested item
          2. second nested item
        3. 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.

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

          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

          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
          • P Offline
            P Offline
            pranap
            wrote on last edited by
            #5

            Thanks !!! It works.

            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