我想在按下菜单按钮时缩进 QPlainTextEdit 的文本。按下按钮时,我询问是否有选择,如果没有,我只是缩进当前行,如果有,我想缩进选择中的所有行。现在代码适用于单行,但是当缩进选择时就像行的最后一部分消失了。例如,如果我有这行: "Artificial Intelligence stands no chance against Natural Stupidity."
,在缩进之后就是:" Artificial Intelligence stands no chance against Natural Stupidi
之后,如果我开始在该行中书写,那么当文本到达现在是句子的结尾时,它就会开始消失。此外,如果我单击或将光标放在该行中消失的句子部分之后,程序会崩溃。
编码:
void MainWindow::on_action_Indent_triggered()
{
Document* doc = dynamic_cast<Document*>(ui->tabsManager->currentWidget());
QTextCursor cursor = doc->textArea->textCursor();
cursor.beginEditBlock();
// If ther is no text selected...
if (cursor.selection().isEmpty()) {
cursor.movePosition(QTextCursor::StartOfLine);
cursor.insertText(this->tabLength);
} else { // If the selection is not empty...
cursor.beginEditBlock();
// Save selection start and end
int start = cursor.selectionStart();
int end = cursor.selectionEnd();
cursor.clearSelection();
// Set end to the end of line of the selected line
cursor.setPosition(end);
cursor.movePosition(QTextCursor::EndOfLine);
end = cursor.position();
// Set cursor to the start of the first selected line
cursor.setPosition(start);
cursor.movePosition(QTextCursor::StartOfLine);
start = cursor.position();
// While still in the selection, add " " to the start of each line
do {
cursor.movePosition(QTextCursor::StartOfLine);
cursor.insertText(this->tabLength);
end += this->tabLength.count();
cursor.movePosition(QTextCursor::EndOfLine);
} while (cursor.position() < end && cursor.movePosition(QTextCursor::Down));
// Select the changed areatabLenght
cursor.clearSelection();
cursor.setPosition(start);
while (cursor.position() < end)
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
}
// Set the cursor in the GUI
doc->textArea->setTextCursor(cursor);
cursor.endEditBlock();
}
Document 是一个 Class,textArea 是一个 QTextPlainEdit。this->tabLength 是一个值为 " " 的 QString