如何重置QML Textarea中丰富文本的样式

发布于 2025-02-08 20:35:56 字数 759 浏览 0 评论 0原文

如果我在QML Textarea中插入丰富的文本,它将保留该样式(文本颜色,下划线等)以获取进一步的输入。有没有办法防止这种情况发生?

示例:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    TextArea {
        id: textArea
        textFormat: Qt.RichText
        focus: true
        selectByMouse: true
        selectByKeyboard: true

        Keys.onPressed: {
            if (event.text === "@") {
                textArea.text += "<a href='mentioned://user'>@User</a>";
                event.accepted = true
            }
        }
    }
}

当我输入“@”字符时,它将插入&lt; a&gt;链接,但是当我继续键入其他字符时,它会将它们附加到链接上,就像它们是其中的一部分一样。如何在不附加到链接(标签)的情况下继续键入新段落?

If I insert rich text in QML TextArea, it will preserve that style (text color, underline etc.) for further inputs. Is there a way to prevent that from happening?

Example:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    TextArea {
        id: textArea
        textFormat: Qt.RichText
        focus: true
        selectByMouse: true
        selectByKeyboard: true

        Keys.onPressed: {
            if (event.text === "@") {
                textArea.text += "<a href='mentioned://user'>@User</a>";
                event.accepted = true
            }
        }
    }
}

When I enter "@" character it will insert the <a> link instead, but when I continue typing other characters it will append them to the link, as if they are a part of it. How can I continue typing a new paragraph, without appending to the link ( tag)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-02-15 20:35:56

标签解决问题后添加一个空格:

Window {
width: 640;
height: 480;
visible: true;
title: qsTr("Code from @Meliodas on StackOverflow");

    TextArea {
        id: textArea;
        textFormat: Qt.RichText;
        focus: true;
        selectByMouse: true;
        selectByKeyboard: true;

        Keys.onPressed: (event) => {
            if (event.text === "@") {
                // HERE
                insert(cursorPosition, "<a href='mentioned://user'>@User</a> ");
                event.accepted = true;
            }
        }
    }
}

请注意,我享有修改其他代码的自由:

  • ​警告告诉我隐式事件参数已弃用

Adding a whitespace after the tag solves the problem :

Window {
width: 640;
height: 480;
visible: true;
title: qsTr("Code from @Meliodas on StackOverflow");

    TextArea {
        id: textArea;
        textFormat: Qt.RichText;
        focus: true;
        selectByMouse: true;
        selectByKeyboard: true;

        Keys.onPressed: (event) => {
            if (event.text === "@") {
                // HERE
                insert(cursorPosition, "<a href='mentioned://user'>@User</a> ");
                event.accepted = true;
            }
        }
    }
}

Note that I took the freedom of modifying other pieces of your code :

  • insert() instead of text += to avoid automatic newlines
  • explicitly defining event to silence a warning telling me implicit event parameters are deprecated
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文