I have rich text items implemented using QGraphicsTextItem
To set font size, for example:
void set (int fontSize) {
QTextCursor _cursor = textCursor();
QTextCharFormat _format;
_format.setFontPointSize(fontSize);
_cursor.mergeCharFormat(_format);
setTextCursor(_cursor); }
A lot more complicated is to read the font size.
Assuming I have a selection, I must iterate through the document, through all QTextBlock
, QTextFragment
, reading the QTextCharFormat
...
But the simple option, if there is no selection, just reading the font size at cursor:
int get () {
return textCursor().charFormat().fontPointSize(); }
This works, but I found 3 issues:
1) Setting font size by QGraphicsTextItem
properties:
QFont f = font();
f.setPointSize(20);
setFont(f);
this returns 0 by my get
function above. To set the font size for the entire item, I have to use the same method as in the set
function.
Shouldn't the setFont
method set a font that can be read from the QTextCursor
?
2) setHtml
can set formatting - but I don't see any way to read that formatting
How can I read the rich text formatting from an html fragment ? Is the only posiblity, parsing the html ?
3) (my current stumbling block)
Copy formatted text from an outside source and paste in the QGraphicsTextItem
seems to maintain the formatting of the source - but how can I read that formatting ?
The get
method above reads font size 0 if the text was pasted from outside.
font().pointSize()
always returns 8. (I have not set it so I imagine that is a default)
Is there another method to read the text format ?
is the clipboard text formatted using html ?
How can I find the font size (or any other formatting) from the pasted text ?
(The same questions apply to block formatting, like alignment).