簡體   English   中英

如何在Qt中使用QSyntaxHighlighter類在QML TextEdit上實現富文本邏輯?

[英]How to implement rich text logic on QML TextEdit with QSyntaxHighlighter class in Qt?

我的QML文件中有一個TextEdit,並且我有一個QSyntaxHighlighter C ++類。 我想在C ++類中指定突出顯示邏輯並將其應用於TextEdit,但是我不確定如何在QML對象和C ++類之間建立連接。 您還可以提供一些示例代碼嗎? 我找不到如何使用Qt文檔來實現它。

您可以使用持有QQuickTextDocument實例的TextEdit::textDocument來訪問對基礎QTextDocument訪問QTextDocument ,您可以將其傳遞給QSyntaxHighlighter構造函數。

如果有人需要對此進行更詳細的說明。 首先,我在自定義C ++類中創建了一個Q_PROPERTY

Q_PROPERTY(QQuickTextDocument* mainTextEdit READ mainTextEdit WRITE setMainTextEdit NOTIFY mainTextEditChanged)

然后,我將textEdit.textDocument分配給textEdit.textDocument中的Q_PROPERTY

customClass.mainTextEdit = textEdit.textDocument

然后,在我的QML中調用initHighlighter() (該函數必須為Q_INVOKABLE ),后者將調用熒光筆類的構造函數並將其傳遞給textEdit的文本文檔。

void initHighlighter()
{
Highlighter *highlighter;
highlighter = new Highlighter(m_mainTextEdit->textDocument());
}

注意:自定義熒光筆類需要從QSyntaxHighlighter派生。

示例實施:

HighlightComponent.h

class HighlightComponent : public QObject
{
Q_OBJECT
    //@formatter:off
    Q_PROPERTY(QString text
                       READ getText
                       WRITE setText
                       NOTIFY textChanged)
    //@formatter:on
    using inherited = QObject;
public:
    explicit HighlightComponent(QObject* parent = nullptr);

    static void registerQmlType();

    const QString& getText()
    {
        return _text;
    }

    void setText(const QString& newText)
    {
        if (newText != _text)
        {
            _text = newText;
            emit textChanged();
        }
    }

    Q_INVOKABLE void onCompleted();

signals:
    void textChanged();

private:
    std::unique_ptr<QSyntaxHighlighter> _highlight;
    QString _text = "";
};

HighlightComponent.cpp

HighlightComponent::HighlightComponent(QObject* parent)
        : inherited(parent)
{
}

void HighlightComponent::onCompleted()
{
    auto property = parent()->property("textDocument");
    auto textDocument = property.value<QQuickTextDocument*>();
    auto document = textDocument->textDocument();
    //TODO init here your QSyntaxHighlighter
}

void HighlightComponent::registerQmlType()
{
    qmlRegisterType<HighlightComponent>("com.HighlightComponent", 1, 0, "HighlightComponent");
}

main.cpp

int main(int argc, char* argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    HighlightComponent::registerQmlType();

    engine.load(QUrl(QStringLiteral("qrc:/view/main.qml")));

    return app.exec();
}

樣本QML

TextArea {
    id: testTextArea
    text: testTextArea.text
    //inputMethodHints: Qt.ImhNoPredictiveText

    onTextChanged: {
        testTextArea.text = text
    }

    HighlightComponent {
        id: testTextArea
        Component.onCompleted: onCompleted()
    }
}

鏈接:

https://wiki.qt.io/How_to_Bind_a_QML_Property_to_a_C%2B%2B_Function

http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

http://wiki.qt.io/Spell-Checking-with-Hunspell

http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

http://doc.qt.io/qt-5/qtwidgets-richtext-syntaxhighlighter-example.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM