qt - 有没有办法用QML TextEdit 的 selectedText 属性中的替代文本替换标签
问题描述
我有一个TextEdit
用于显示短信的组件。解析消息时,我使用我的组件上的<img>
标签和设置将常用的表情符号快捷方式替换为表情符号。当有人复制包含表情符号的消息时,属性插入一个 OBJECT REPLACEMENT CHARACTER 0xFFFC 而不是选定的标签。textFormat: TextEdit.RichText
TextEdit
selectedText
TextEdit
<img>
例子:
import QtQuick 2.9
import QtQuick.Controls 2.2
Item {
id: root
width: 500
height: 500
TextEdit {
id: myTextEdit
anchors.centerIn: parent
text: "Test <img src=\"app.icns\"></img> image"
textFormat: TextEdit.RichText
readOnly: true
selectByMouse: true
onSelectedTextChanged: {
console.log(selectedText);
}
}
}
当您选择其中的所有内容时,myTextEdit
它会打印“Test  image”。有没有办法指定一些其他的替换方式<img>
,selectedText
所以我可以选择一些替代文本而不是 0xFFFC。
解决方案
您可以使用selectionStart
andselectionEnd
来获取该范围内的实际文本,text
而不是依赖selectedText
.
就像是:
console.log(text.slice(selectionStart, selectionEnd));
推荐阅读
- c++ - 构建选项已更改,重新构建所有退出状态 1 [错误] 退出代码 = 1 MXChip Azure IoT 开发工具包
- javascript - 克隆 jquery 按钮两次并让他们两个都在做同样的任务
- java - Spring Boot意外重复bean
- java - 将图库中的图片保存在我的文件夹外部 SD 中?
- c# - 静态/全局变量的内存隔离(多次实例化)
- hyperledger-fabric - 如何从 fabric-client 获取用户名,名称在 registerUser.js 中设置?
- c# - 在附加到新的父 Visual 之前,必须断开指定子与当前父 Visual 的连接
- kubernetes-helm - 在 Helm 图表中使用 Requirements.lock 文件
- windows - 订购标准错误和标准输出或刷新标准错误
- javascript - 如何在 Angular 2+ 项目中使用 intl-tel-input 插件 utilsscript?