I have a QTextEdit
that displays text with multiple fonts. I want to get each character (or text block) with its font information.
I've tried getting the QTextBlock
's for the QTextDocument
. But it seems to be a string with ontype of font.
Is there a way to get the fonts of a QTextEdit
?
You should be able to get this information from the 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();
}