如何在 Qt 中使用 QSyntaxHighlighter 类在 QML TextEdit 上实现富文本逻辑?
我的 QML 文件中有一个 TextEdit,我有一个 QSyntaxHighlighter C++ 类.我想在 C++ 类中指定突出显示逻辑并将其应用于 TextEdit,但我不确定如何在 QML 对象和 C++ 类之间建立连接.您还可以提供一些示例代码吗?我找不到如何使用 Qt 文档来实现它.
I have a TextEdit in my QML file and I have a QSyntaxHighlighter C++ class. I want to specify the highlighting logic in the C++ class and apply it to the TextEdit, but I am not sure how to make the connection between the QML object and the C++ class. Can you also please provide some sample code? I couldn't find how to implement it with the Qt documentation.
推荐答案
你可以使用TextEdit::textDocument
,它包含 QQuickTextDocument
,获取对底层 QTextDocument
的访问权限,您可以将其传递给 QSyntaxHighlighter
构造函数.
You can use Text:textDocument
, which holds an instance of QQuickTextDocument
, to gain access to the underlying QTextDocument
that you can pass to QSyntaxHighlighter
constructor.
相关文章