C++ 将自定义HTML类特性添加到QTextEdit中的选定块

C++ 将自定义HTML类特性添加到QTextEdit中的选定块,c++,css,qt,qtextedit,C++,Css,Qt,Qtextedit,我对Qt并不陌生,但我找不到如何在QTextEdit中将自定义css类添加到选定的块中 据我所知,格式是通过以下代码更改的: QTextCursor cursor = textEdit->textCursor(); QTextBlockFormat bfmt; // Apply format changes cursor.setBlockFormat(bfmt); 执行此操作时,生成的HTML代码将创建一个带有内联样式的范围,但我希望插入css类: <SPAN class='myc

我对Qt并不陌生,但我找不到如何在QTextEdit中将自定义css类添加到选定的块中

据我所知,格式是通过以下代码更改的:

QTextCursor cursor = textEdit->textCursor();
QTextBlockFormat bfmt;
// Apply format changes
cursor.setBlockFormat(bfmt);
执行此操作时,生成的HTML代码将创建一个带有内联样式的范围,但我希望插入css类:

<SPAN class='myclass'>text</span>
文本

我缺少QTextBlockFormat中用于设置文本css类的函数。

您应该能够通过向所选文本手动添加
标记来模拟此行为:

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));
QString oldText=cursor.selectedText();
//不是连接三个字符串的最佳方法,但仅限于。。。
cursor.insertHtml(QString(“%2”).arg(“myclass”).arg(oldText));
selectedText()
将返回当前选定的文本,而
insertHtml()
将在光标旁边插入新文本,删除当前选择(如果有)