我有一个QTextEdit
显示具有多种字体的文本。我想获取每个字符(或文本块)及其字体信息。我试过QTextBlock
为QTextDocument
. 但它似乎是一个带有 ontype 字体的字符串。
有没有办法获取 a 的字体QTextEdit
?
您应该能够从以下位置获取此信息QTextDocument
:
QTextDocument doc;
QTextBlock currentBlock = doc.firstBlock();
while (currentBlock.isValid()) {
QTextBlockFormat blockFormat = currentBlock.blockFormat();
QTextCharFormat charFormat = currentBlock.charFormat();
QFont font = charFormat.font();
// each QTextBlock holds multiple fragments of text, so iterate over it:
QTextBlock::iterator it;
for (it = currentBlock.begin(); !(it.atEnd()); ++it) {
QTextFragment currentFragment = it.fragment();
if (currentFragment.isValid()) {
// a text fragment also has a char format with font:
QTextCharFormat fragmentCharFormat = currentFragment.charFormat();
QFont fragmentFont = fragmentCharFormat.font();
// etc...
}
}
currentBlock = currentBlock.next();
}