无法对TextEdit进行高亮显示



TL;DR:TextEdit只在我点击时绘制突出显示的文本。没有任何帮助

我有一个ListView和一个具有字符串属性的QAbstractListModel模型。这些字符串属性正在进行拼写检查,QSyntaxHighlighter用于显示拼写错误。我在TextEditComponent.onCompleted中创建了QSyntaxHighlighter的后代。我仔细检查了高亮显示get在正确拼写错误的情况下执行,Highlighter的setFormat()在正确位置执行。问题是,只有当我单击TextEdit本身时,它才会用红色绘制文本(无效)。

TextEdit住在Flickable(用于跟踪光标)中,Flickable住在Rectangle(用于具有良好的背景和边界)中。绑定到某些信号并调用TextEdit的update()没有帮助。

拼写检查完成后,我发出创建的SyntaxHighlighter的rehighlight()信号。

Rectangle {
  id: descriptionRect
  height: 30
  border.width: descriptionTextInput.activeFocus ? 1 : 0
  clip: true
  Flickable {
      id: descriptionFlick
      contentWidth: descriptionTextInput.paintedWidth
      contentHeight: descriptionTextInput.paintedHeight
      anchors.fill: parent
      interactive: false
      flickableDirection: Flickable.HorizontalFlick
      height: 30
      clip: true
      focus: false
      function ensureVisible(r) {
          if (contentX >= r.x)
              contentX = r.x;
          else if (contentX+width <= r.x+r.width)
              contentX = r.x+r.width-width;
      }
      TextEdit {
          id: descriptionTextInput
          width: descriptionFlick.width
          height: descriptionFlick.height
          text: description
          onTextChanged: model.editdescription = text
          Component.onCompleted: {
              globalModel.initDescriptionHighlighting(index, descriptionTextInput.textDocument)
          }
          onCursorRectangleChanged: descriptionFlick.ensureVisible(cursorRectangle)
         }
     }
 }

以下是一个项目的小样本,演示了在点击文本之前它是如何不起作用的https://bitbucket.org/ribtoks/qt-highlighting-issue

你知道我该怎么解决这个问题吗?

刚刚在5.11.2遇到此问题,并找到了以下修复程序,该修复程序允许更新单个块,而无需突出显示/取消选择整个文本区域

rehighlightBlock(newBlock);
Q_EMIT document()->documentLayout()->updateBlock(newBlock);

该问题可能是由QTBUG-44765引起的,已在Qt 5.5中修复。

考虑到bug的低级别,我认为解决它实际上并不可行。

当你完成语法高亮显示时,你可以通过在TextEdit中添加一个空字符串来解决这个问题

TextEdit {
    id: captionTextEdit
    width: wrapperFlick.width
    height: wrapperFlick.height
    text: display
    readOnly: true
    Component.onCompleted: {
        itemsModel.initHighlighter(index, captionTextEdit.textDocument)
    }
    Connections {
        target: itemsModel
        onUpdateTextEdit: {
            console.log("Update element at index: " + indexToUpdate)
            if (indexToUpdate == index)
            {
                console.log("Update me!")
                captionTextEdit.append("")
            }
        }
    }
    onCursorRectangleChanged: wrapperFlick.ensureVisible(cursorRectangle)
}

其中updateTextEdit(indexToUpdate)是itemsModel必须发出的新信号。

项目模型.h

signals:
    void updateTextEdit(int indexToUpdate);

itemsmodel.cpp

void ItemsModel::initHighlighter(int index, QQuickTextDocument *document) {
    // Signal mapper could be avoided if lamda slot are available (Qt5 and C++11)
    QSignalMapper* signalMapper = new QSignalMapper(this);
    if (0 <= index && index < m_ItemsList.length()) {
        SingleItem *item = m_ItemsList.at(index);
        SpellCheckHighlighter *highlighter = new SpellCheckHighlighter(document->textDocument(), item);
        QObject::connect(item, SIGNAL(spellCheckResultsReady()),
                         highlighter, SLOT(rehighlight()));
        // TODO: Don't connect this slot for Qt 5.5+ to avoid performance overhead
        QObject::connect(item, SIGNAL(spellCheckResultsReady()),
                         signalMapper, SLOT(map()));
        signalMapper->setMapping(item, index);
    }
    connect(signalMapper, SIGNAL(mapped(int)),
            this, SIGNAL(updateTextEdit(int)));
}

此处提供完整代码:https://bitbucket.org/swarta/rehighlighdemo/branch/workaround#diff

最新更新