2013-02-08 78 views
3

我對Qt並不陌生,但我找不到如何將自定義css類添加到QTextEdit中的選定塊。將自定義HTML類屬性添加到QTextEdit中的選定塊

據我所知,格式改變絲毫這樣的代碼:

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

當我這樣做時,生成的HTML代碼創建了它內聯風格的跨度,但我要的是插入css類:

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

我在QTextBlockFormat中缺少一個函數來設置文本的css類。

回答

0

您應該能夠通過手動添加<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()將在光標的beggining插入新的文本,刪除當前選擇,如果有的話。

相關問題