如何重置QML Textarea中丰富文本的样式
如果我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
标签解决问题后添加一个空格:
请注意,我享有修改其他代码的自由:
事件
参数已弃用Adding a whitespace after the tag solves the problem :
Note that I took the freedom of modifying other pieces of your code :
text +=
to avoid automatic newlinesevent
to silence a warning telling me implicitevent
parameters are deprecated