无法在TextEdit中开启高亮显示功能

8
TL;DR: 当我点击它时,TextEdit 才会绘制高亮文本。无法通过其他任何方法解决。
我有一个带有 QAbstractListModel 模型和字符串属性的 ListView。 这些字符串属性正在进行拼写检查,并使用 QSyntaxHighlighter 显示拼写错误。在 Component.onCompleted 中创建了一个 TextEditQSyntaxHighlighter 子类。我仔细检查了高亮显示是在正确的拼写错误下执行的,并且 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。您有什么解决办法吗?

1
我已经构建并执行了您的源代码,程序按照您的期望工作。这些单词是在我点击拼写检查按钮后突出显示的。我在OSX上使用了Qt 5.5.1。 - DenimPowell
一切都按照你的预期工作,尝试使用'make disclean && qmake && make'重新构建项目。 - swex
肯定不能在Qt 5.4上工作。无论是否清理和qmake都不行。它可能在5.5.1上工作。但是我需要保持我的产品与Qt 5.2的兼容性,这需要一些技巧,以及5.4。 - Ribtoks
2个回答

1

我在5.11.2版本中遇到了这个问题,并找到了以下解决方法,可以更新单个块而无需突出/取消选择整个文本区域

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

嗨,非常感谢这个更新。你尝试过在示例中修补小的代码文件吗?(以确认它是否有效) - Ribtoks

1
问题可能是由QTBUG-44765引起的,已在Qt 5.5中修复。
考虑到这个错误的低级别,我认为实际上没有必要解决它。
当您完成语法突出显示时,可以通过将空字符串附加到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需要发出的新信号。

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


嗨,Simon。我最终采用了相同的解决方法。只是不使用append(""),而是使用deselect()方法。实际上,我正在使用Qt 5.5.1,在我的复杂应用程序中,这仍然无法正常工作(需要解决问题),而在演示中,它可以在5.5.1中正常工作。无论如何,我仍然没有打算用我的方法回答这个问题,所以我会接受你的答案。 - Ribtoks
好的,很棒。所以我们有一个好的解决方案供其他人找到和投票支持。您的演示项目为所有想要在QML中开始进行语法高亮显示的人提供了很多价值,因此保持它的活力将是件好事。 - Simon Warta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接