Qt程序崩溃QTextEdit通过QTextCursor修改

时间:2012-05-09 03:03:35

标签: c++ qt4

我想按下菜单按钮缩进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-&gt; tabLength是一个QString,其值为“”

1 个答案:

答案 0 :(得分: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"