How to remove blank space of selected lines from QTextEdit
-
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(); }
-
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(); }
-
@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?@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.
-
@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.
@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? -
@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?@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(); }
-