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. How to remove blank space of selected lines from QTextEdit
Forum Updated to NodeBB v4.3 + New Features

How to remove blank space of selected lines from QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 595 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.
  • S Offline
    S Offline
    sayou678
    wrote on last edited by sayou678
    #1

    I have a QTextEdit, when I set the text style, for example QTextListFormat::ListCircle, I found the blank space before the line is kept, How to remove these blank space?

    this is my text:

    • aa
    • [][][][]bb[][]
    • [][]cc

    I want this:

    • aa
    • bb[][]
    • cc

    Here is my code:

    void RichText::changeStyle(QTextListFormat::Style style)
    {
        QTextCursor cursor = ui->textEdit->textCursor();    
        cursor.beginEditBlock();    
    
        QTextBlockFormat blockFmt = cursor.blockFormat();
        cursor.setBlockFormat(blockFmt);
        QTextListFormat listFmt;
        if (cursor.currentList()) {
            listFmt = cursor.currentList()->format();
        } else {
            listFmt.setIndent(blockFmt.indent() + 1);
            blockFmt.setIndent(0);
            cursor.setBlockFormat(blockFmt);
        }
    
        auto curStyle = listFmt.style();
        if(curStyle == style)
            listFmt.setStyle(QTextListFormat::ListStyleUndefined);
        else
            listFmt.setStyle(style);
    
        cursor.createList(listFmt);
    
        cursor.endEditBlock();
    }
    
    JonBJ 1 Reply Last reply
    0
    • S sayou678

      I have a QTextEdit, when I set the text style, for example QTextListFormat::ListCircle, I found the blank space before the line is kept, How to remove these blank space?

      this is my text:

      • aa
      • [][][][]bb[][]
      • [][]cc

      I want this:

      • aa
      • bb[][]
      • cc

      Here is my code:

      void RichText::changeStyle(QTextListFormat::Style style)
      {
          QTextCursor cursor = ui->textEdit->textCursor();    
          cursor.beginEditBlock();    
      
          QTextBlockFormat blockFmt = cursor.blockFormat();
          cursor.setBlockFormat(blockFmt);
          QTextListFormat listFmt;
          if (cursor.currentList()) {
              listFmt = cursor.currentList()->format();
          } else {
              listFmt.setIndent(blockFmt.indent() + 1);
              blockFmt.setIndent(0);
              cursor.setBlockFormat(blockFmt);
          }
      
          auto curStyle = listFmt.style();
          if(curStyle == style)
              listFmt.setStyle(QTextListFormat::ListStyleUndefined);
          else
              listFmt.setStyle(style);
      
          cursor.createList(listFmt);
      
          cursor.endEditBlock();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @sayou678
      I see no reason why Qt should remove leading (or trailing) spaces in text that you give it on anything, or because it's put in a list. If you want to strip these do so yourself?

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @sayou678
        I see no reason why Qt should remove leading (or trailing) spaces in text that you give it on anything, or because it's put in a list. If you want to strip these do so yourself?

        S Offline
        S Offline
        sayou678
        wrote on last edited by
        #3

        @JonB because my client asked me to remove the leading spaces when it's put in a list. I tried to move the cursor to line start by QTextCursor::setPosition, then check if current character is space, if it is, then remove it by QTextCursor::deleteChar. But I can't get the method to check if current character is space.

        JonBJ 1 Reply Last reply
        0
        • S sayou678

          @JonB because my client asked me to remove the leading spaces when it's put in a list. I tried to move the cursor to line start by QTextCursor::setPosition, then check if current character is space, if it is, then remove it by QTextCursor::deleteChar. But I can't get the method to check if current character is space.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @sayou678
          I had in mind removing the spaces before you put the text into the document list. But if you need to do once in a document I think you use int QTextCursor::position() const to get an integer and pass that to QChar QTextDocument::characterAt(int pos) const to read the character there?

          S 1 Reply Last reply
          1
          • JonBJ JonB

            @sayou678
            I had in mind removing the spaces before you put the text into the document list. But if you need to do once in a document I think you use int QTextCursor::position() const to get an integer and pass that to QChar QTextDocument::characterAt(int pos) const to read the character there?

            S Offline
            S Offline
            sayou678
            wrote on last edited by
            #5

            @JonB it works, thanks. Here is the final code

            void RichText::changeStyle(QTextListFormat::Style style)
            {
                QTextCursor cursor = ui->textEdit->textCursor();
            
                cursor.beginEditBlock();
            
                QTextBlockFormat blockFmt = cursor.blockFormat();
                cursor.setBlockFormat(blockFmt);
                QTextListFormat listFmt;
                if (cursor.currentList()) {
                    listFmt = cursor.currentList()->format();
                } else {
                    listFmt.setIndent(blockFmt.indent() + 1);
                    blockFmt.setIndent(0);
                    cursor.setBlockFormat(blockFmt);
                }
            
                auto curStyle = listFmt.style();
                if(curStyle == style)
                    listFmt.setStyle(QTextListFormat::ListStyleUndefined);
                else
                {
                    auto startPos = cursor.selectionStart();
                    auto endPos = cursor.selectionEnd();
                    auto totalLength = ui->textEdit->toPlainText().length();
            
                    cursor.setPosition(startPos);
                    while(true)
                    {
                        cursor.movePosition(QTextCursor::StartOfLine);
                        if((ui->textEdit->toPlainText().length() - cursor.position()) < (totalLength - endPos))
                            break;
            
                        //delete leading space
                        while(true)
                        {
                            auto curChar = cursor.document()->characterAt(cursor.position());
                            qDebug() << curChar;
                            if(curChar == ' ')
                                cursor.deleteChar();
                            else
                                break;
                        }
            
                        //move to next line
                        if(!cursor.movePosition(QTextCursor::Down))
                            break;
                    }
            
                    cursor.setPosition(startPos, QTextCursor::MoveAnchor);
                    cursor.setPosition(ui->textEdit->toPlainText().length() - (totalLength - endPos), QTextCursor::KeepAnchor);
                    ui->textEdit->setTextCursor(cursor);
            
                    listFmt.setStyle(style);
                }
            
            
                cursor.createList(listFmt);
            
                cursor.endEditBlock();
            }
            
            1 Reply Last reply
            1
            • S sayou678 has marked this topic as solved on

            • Login

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