Qt how to move selection to one line up

532 views Asked by At

I have a trouble. Here my code is:

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

When user select some text and click on button "Line up" this function is called. Every line which has selection should move to one line up. But after that undo stack have to steps: remove text and paste text. What should I do? I want to make it simple operation in one step.

0

There are 0 answers