我遇到了麻烦。我的代码是:
void TextEditor::moveToLineUp()
{
QTextCursor cur = textCursor();
if(cur.hasSelection()) {
int start = cur.selectionStart();
int end = cur.selectionEnd();
QTextBlock startBlock = document()->findBlock(start);
QTextBlock endBlock = document()->findBlock(end);
cur.setPosition(startBlock.position());
cur.setPosition(endBlock.position()+endBlock.length(), QTextCursor::KeepAnchor);
} else {
// I'll fill it later
}
// ??? I don't know how not to write remove action in undo stack
QString text = cur.selectedText();
cur.removeSelectedText();
QTextCursor ncur(cur.block().previous());
ncur.insertText(text);
}
当用户选择一些文本并单击“Line up”按钮时,将调用此函数。 选择的每一行都应移到一行。但在此之后,撤消堆栈必须采取步骤:删除文本并粘贴文本。我该怎么办?我想一步到位地完成简单的操作。