How to remove blank space of selected lines from QTextEdit

115 Views Asked by At

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();
}
2

There are 2 best solutions below

0
Brett Li On BEST ANSWER

I figure it out using the following 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();
}
9
Yanis600 On

You can use QString::trimmed in order to remove additional blank spaces.