【问题标题】:Add custom HTML class property to selected block in a QTextEdit将自定义 HTML 类属性添加到 QTextEdit 中的选定块
【发布时间】:2013-02-08 11:53:13
【问题描述】:

我不是 Qt 新手,但我不知道如何将自定义 css 类添加到 QTextEdit 中的选定块。

据我所知,格式更改为如下代码:

QTextCursor cursor = textEdit->textCursor();
QTextBlockFormat bfmt;
// Apply format changes
cursor.setBlockFormat(bfmt);

当我这样做时,生成的 HTML 代码会在其中创建一个带有内联样式的跨度,但我想要的是插入 css 类:

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

我在 QTextBlockFormat 中缺少一个函数来设置文本的 css 类。

【问题讨论】:

    标签: c++ css qt qtextedit


    【解决方案1】:

    您应该能够通过手动将&lt;span style=""&gt; 标签添加到您选择的文本来模拟这种行为:

    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() 将在光标开头插入新文本,删除当前选择(如果有)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多