将自定义HTML类特性添加到QTextEdit中的选定块
Add custom HTML class property to selected block in a QTextEdit
我不是Qt的新手,但我找不到如何在QTextEdit中向所选块添加自定义css类。
据我所知,格式被这样的代码改变了:
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++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何在QTextEdit中添加URL或IMAGE-URL
- 在 QTextEdit C++ 中为特定行添加下划线
- 如何将文本添加到QTextEdit的顶部
- Qt QTextEdit添加虚假行
- 配置QTextEdit的包装以添加缩进
- 将QTextEdit对象添加到QMainWindow中
- 使用QPushButton将文本添加到QTextEdit
- 将自定义HTML类特性添加到QTextEdit中的选定块