如何在Qt中使用QSyntaxHighlighter类在QML TextEdit上实现富文本逻辑? [英] How to implement rich text logic on QML TextEdit with QSyntaxHighlighter class in Qt?
问题描述
我有一个TextEdit在我的QML文件,我有一个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.
推荐答案
如果有人需要更详细的解释。
首先,我在自定义C ++类中创建了一个 Q_PROPERTY 。 / p>
In case someone needs a more detailed explanation for this. First, I created a Q_PROPERTY inside a custom C++ class.
Q_PROPERTY(QQuickTextDocument* mainTextEdit READ mainTextEdit WRITE setMainTextEdit NOTIFY mainTextEditChanged)
然后我将 textEdit.textDocument
分配给 Q_PROPERTY 。
customClass.mainTextEdit = textEdit.textDocument
然后我调用 initHighlighter c>(该函数必须是 Q_INVOKABLE )在我的QML中调用我的荧光笔类的构造函数,并传递 textEdit 的文本文档, a>。
Then I call initHighlighter()
(the function has to be Q_INVOKABLE) in my QML which calls the constructor of my highlighter class and passes it the text document of the textEdit.
void initHighlighter()
{
Highlighter *highlighter;
highlighter = new Highlighter(m_mainTextEdit->textDocument());
}
注意:自定义高亮显示类需要从 QSyntaxHighlighter 。
Note: The custom highlighter class needs to be derived from QSyntaxHighlighter.
这篇关于如何在Qt中使用QSyntaxHighlighter类在QML TextEdit上实现富文本逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!