0

問題があります: 私のプロジェクトは QTextEdit と QSyntaxHighlighter を備えた非常に単純なものです。線を強調するように頼みます。

次の図は、問題を示しています。

ここに画像の説明を入力

アプリケーションの関連コードは次のとおりです。

void MainWindow::openFile(const QString &path)
{
    QString fileName = path;

    if (fileName.isNull())
        fileName = QFileDialog::getOpenFileName(this,
            tr("Open File"), "", "C++ Files (*.cpp *.h)");

    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (file.open(QFile::ReadOnly | QFile::Text))
            editor->setPlainText(file.readAll());

        QVector<quint32> test;
        test.append(8); // I want the eighth line to be highlighted
        editor->highlightLines(test);
    }
}

#include "texteditwidget.h"

TextEditWidget::TextEditWidget(QWidget *parent) :
    QTextEdit(parent)
{
    setAcceptRichText(false);
    setLineWrapMode(QTextEdit::NoWrap);

}



// Called to highlight lines of code
void TextEditWidget::highlightLines(QVector<quint32> linesNumbers)
{

    // Highlight just the first element
    this->setFocus();
    QTextCursor cursor = this->textCursor();
    cursor.setPosition(0);
    cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, linesNumbers[0]);
    this->setTextCursor(cursor);
    QTextBlock block = document()->findBlockByNumber(linesNumbers[0]);
    QTextBlockFormat blkfmt = block.blockFormat();
    // Select it
    blkfmt.setBackground(Qt::yellow);
    this->textCursor().mergeBlockFormat(blkfmt);
}

ただし、私が使用した cpp ファイル (ディレクトリ FileToOpen\diagramwidget.cpp 内) を使用してプロジェクトをテストする場合は、完全なソースを次に示します。

http://idsg01.altervista.org/QTextEditProblem.zip

私はこれを長い間解決しようとしてきましたが、これはバグや類似のものではないのではないかと思い始めています

4

2 に答える 2

1

1 枚でこのQTextEditような大量のテキストを受け入れることはできません。たとえば、次のように分割します。

if (!fileName.isEmpty()) {
    QFile file(fileName);
    if (file.open(QFile::ReadOnly | QFile::Text))
    {
        QByteArray a = file.readAll();

        QString s = a.mid(0, 3000);//note that I split the array into pieces of 3000 symbols.
        //you will need to split the whole text like this. 
        QString s1 = a.mid(3000,3000);

        editor->setPlainText(s);
        editor->append(s1);
    }
于 2012-07-31T15:47:09.207 に答える
0

QTextEdit コントロールは、ロードするたびに時間がかかるようです. after を設定するQApplication:processEvents();setPlainText()問題が解決しますが、エレガントな解決策ではありません.

于 2012-07-31T17:08:29.457 に答える