我不是 Qt 的新手,但我找不到如何将自定义 css 类添加到 QTextEdit 中的选定 block 。
据我所知,格式是用这样的代码改变的:
QTextCursor cursor = textEdit->textCursor();
QTextBlockFormat bfmt;
// Apply format changes
cursor.setBlockFormat(bfmt);
当我这样做时,生成的 HTML 代码创建了一个带有内联样式的 span,但我想要的是插入 css 类:
<SPAN class='myclass'>text</span>
我在 QTextBlockFormat 中缺少一个函数来设置文本的 css 类。
最佳答案
您应该能够通过手动添加 <span style="">
来模拟此行为标记到您选择的文本:
QString oldText = cursor.selectedText();
// not the best way to concat three strings, but for example only...
cursor.insertHtml(QString("<span class=\"%1\">%2</span>").arg("myclass").arg(oldText));
selectedText()
将返回当前选定的文本,并且 insertHtml()
将在光标的开头插入新文本,删除当前选择(如果有)。
关于c++ - 将自定义 HTML 类属性添加到 QTextEdit 中的选定 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14771962/