1

我有一个QTextEdit显示具有多种字体的文本。我想获取每个字符(或文本块)及其字体信息。我试过QTextBlockQTextDocument. 但它似乎是一个带有 ontype 字体的字符串。

有没有办法获取 a 的字体QTextEdit

4

1 回答 1

3

您应该能够从以下位置获取此信息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();
}
于 2013-08-26T11:47:42.320 回答