0

我想在按下菜单按钮时缩进 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

4

1 回答 1

4

问题很简单:你打电话beginEditBlock()的次数比你打电话的次数多endEditBlock()。之后立即删除beginEditBlock()呼叫} else {。我可以重现这个问题,并且匹配的[begin|end]EditBlock()调用确实可以解决它。

下面是一个独立的例子。

# indenttest.pro
CONFIG += qt gui
SOURCES += indenttest.cpp
// indenttest.cpp
#include <cmath>
#include <QWidget>
#include <QVBoxLayout>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QTextCursor>
#include <QTextDocumentFragment>
#include <QApplication>

//

class QPlainTextEdit;
class MainWindow : public QWidget
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);

public slots:
    void on_indent();

private:
    const QString tabLength;
    QPlainTextEdit * textArea;
};

//

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    tabLength("    ")
{
    QVBoxLayout * layout = new QVBoxLayout(this);
    QPushButton * btn = new QPushButton("Indent", this);
    layout->addWidget(btn);
    textArea = new QPlainTextEdit(this);
    textArea->setPlainText("foo\nbar\nbaz\nbat");
    layout->addWidget(textArea);
    connect(btn, SIGNAL(clicked()), SLOT(on_indent()));

}

void MainWindow::on_indent()
{
    QTextCursor cursor = 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
    textArea->setTextCursor(cursor);
    cursor.endEditBlock();
}

int main(int argc, char** argv)
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

#include "indenttest.moc"
于 2012-05-29T00:41:48.813 回答