2

对不起,如果这是一个新手问题。我有一些用于在编辑器中标记错误的代码,但当错误消失时,我找不到如何清除标记。这是我为呈现错误而调用的方法。

void Editor::highlightErrors( Thing* t )
{
    if ( !t )
        return; 

    const std::vector<Thing::Error>& errors = t->errors();

    QTextCursor tc = textCursor();
    tc.select(QTextCursor::Document);

    QList<QTextEdit::ExtraSelection> extraSelections;
    for(int i = 0; i < errors.size(); ++i) {
        const Thing::Error& error = errors[i];
        QTextEdit::ExtraSelection sel;
        sel.format = this->errorFormat();
        sel.format.setToolTip(QString(error.error.c_str()));
        sel.cursor = tc;
        sel.cursor.clearSelection();
        sel.cursor.setPosition(error.startPos);
        sel.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, error.endPos - error.startPos);
        sel.cursor.mergeCharFormat(sel.format);
        extraSelections.append(sel);
    }
    setExtraSelections(extraSelections);
}

我希望 QPlainTextEdit 在没有错误的情况下调用此方法时重新绘制没有错误格式的文本,但事实并非如此。QTextLayout::draw 被正确调用,额外的选择也被清除,但文本仍在以错误格式绘制。

谢谢您的帮助。

4

1 回答 1

2

Seems more like a Qt bug to me.

If your just looking for a fix / workaround:

Add this after tc.select(QTextCursor::Document);:

  tc.setCharFormat(textCursor().charFormat());

That should sort the issue out.

Details:

This is the function I was testing this with. I just made the function take an int num and format those char positions as an extraSelection with custom format(Does not include fix I mentioned above).

void MainWindow::highlightErrors(int num) {
  QTextCursor tc = ui_.textEdit->textCursor();
  tc.select(QTextCursor::Document);
  QTextCharFormat format;
  QColor lineColor = QColor(Qt::red);
  format.setBackground(lineColor);
  // format.setFontCapitalization(QFont::AllLowercase);
  QList<QTextEdit::ExtraSelection> extraSelections;
  for(int i = 0; i < num; ++i) {
    QTextEdit::ExtraSelection selection;
    selection.format = format;
    selection.format.setToolTip(QString::number(i));
    selection.cursor = tc;
    selection.cursor.clearSelection();
    selection.cursor.setPosition(i);
    selection.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 1);
    // selection.cursor.mergeCharFormat(selection.format);
    extraSelections.append(selection);
  }
  ui_.textEdit->setExtraSelections(extraSelections);
}

In the above code 2 lines are commented. All that happens is a red background gets applied. Now this works fine.

However if we uncomment format.setFontCapitalization(QFont::AllLowercase);, and re-run we can see the background work as expected but the lowercase change does not get applied(make sure it's caps to begin-with) (This is what I reckon is the Qt bug since format changes only apply partially)

Now if we also uncomment selection.cursor.mergeCharFormat(selection.format);, we can see the lowercase format is applied as well but the format removal stops working.

Hence us needing a fix to reset the CharFormat() on entire document when errorSelection() is changed.

于 2013-04-22T14:14:53.150 回答