How to determine the font of a QTextBlock - c++

Let's say a user pastes some random text into a QTextEdit. How do you determine which font that text is in. For example, Chinese.
I've tried getting the QTextCharFormat for the block. But it seems to be empty of information.
Querying for fontFamily() gets me an empty string.
I've also tried to use the QTextLayout - but that seems to get me the default layout for the document, not for the specific text block.
Thanks for any pointers.

It seems you have to go to iterate over the QTextFragment's inside your block.
The fragments are what contain the specific font information.

Related

Is it possible to add text instead of replacing the entire text for a static text control on MFC?

I know the classic way to show text on a static text control.
GetDlgItem(IDC_STATIC_YOURTEXT)->SetWindowText("What you want to show");
But if my text is gradually added, doesn't have to replace the entire old text. Replacing entire would be low performance if the text is long. Is there any way to add text gradually.
No, there is no such way. If you want to add to a text, keep your own variable, add to it and then call SetWindowText with your variable again when it changes.
If that is too slow, maybe a static text control is not the best way to achieve your goal.

Does XSLFO support fixed layout?

I am using xslfo to generate PDF from my XML file, but whenever I edit something in my source documents, it will impact on pagination of output, that cause some indexing issue that runs according to there appearance.
Are there any attributes or elements to handle or to fix this behaviour?
I assume you have the following situation:
Initially, a page is almost filled with text.
The text is edited and becomes longer. Now it doesn't fit on one page any more.
You want to know if there's a way to automatically change the formatting so the text will fit on one page again.
Unfortunately you can't do this with XSL-FO alone. As far as I know, there is no way to specify "this block of text has to fit on one page, and if it doesn't fit, make the font size smaller until it fits".
You'd have to do some post-processing, along the line of 'count the pages in the PDF, if the page count is larger than X, change a variable in the FO template to make the text smaller and render again'.

QTextEdit: typing in HTML/richttext

I have a QTextEdit and want the user to be able to type rich text which will then automatically be (correctly) shown in the widget (so: formatted).
It works fine when setting the text programmatically (using setText()), but not when manually typed. See picture below.. "Input" is set using setText, the following line is manually typed. I would like this line to automatically be formatted a
What's the (easiest) way to do this? The only way I can think about it to manually catch key events and explicitly set the text as HTML.. But I'm sure there's a better way.
Manual typed html gets escaped, the < will become a < etc . .
You wouldn't be able to edit it if that would not be the case, for obvious reasons.
You could try adding a [render] button or something like that to render the entered text to html. Trying to render on keypress is very dangerous because it makes it terribly inconvenient and counter-intuitive to type something and then have it magically change the output. Also un-finished markup will probably throw a stick in your wheel.
Also pasting from a rich text source (for example a webpage) keeps the formatting.
As "the JinX" already said it will not be so intuitive if you try to capture every key event and then try to change the text to render in HTML.
Though you can use some special key sequences, say "shift+return key" to change the text of current line/entire textedit to to html formatted one.
This is just a suggestion.
In this case more than implementation it is also about what a user will expect.
Changing the text of 1 line/entire textedit from plain to HTML would be easy to achieve as well.

Hide label text for Qt tabs without setting text to empty string

I need a QTabWidget with icons only:
How can I hide the label text of a tab in Qt? I cannot set the text to an empty string (""), as I am using docked widgets ( QDockWidget ) and the label text is set automatically (and I need it if the widget is floating).
But in tabbed mode I just want to display the icons (of the tabs).
Possible approaches:
Font size to 0?
I need to create my own bar class and override the paint event as here
Anything easier / cleaner?
--- Edit ---
Ok, the "set window title to empty string, and reset it the original text" approach works. I am using the topLevelChanged signal for this. However, it has some drawbacks, as the empty text still occupies some space. Another issue, with the text the tooltip is gone, and I cannot set it back.
What I am currently trying is something in-between the "text empty" and Prasad Silva's approach. I try to identify the text label inside the tab and set its size to 0, then reset it. It's slightly different, but would keep the text intact.
Btw, I see a line on top of my tabs, any idea what this is (where it comes from)?
Edit: There seems to be no "easy way" (style sheet, attribute) for this, see Hiding bottom line in QTabBar
Maybe I will create the whole tab bar on my own, as the automatically generated stuff is just too hard to handle (agree with PS on this).
This can not be done easily. Use empty text.
The way I solved something like was to create a QDockWidget subclass that installed a QWidget subclass as the titlebar (via setTitleBarWidget). This gave me control over showing/hiding the text in the titlebar when the dock widget fires topLevelChanged, dockLocationChanged and visiblityChanged.
This is really a big hack to get around the fact that Qt has refused to expose a public API for the docking system. We have since moved on to a custom docking implementation due to these limitations.
If you do not want to see the text, you can set it to an empty text after saving the current text, and when you want to see it again, restore it from the stored variable.
I do not think there is anything in the API for this not so common case, which means you will need to do it yourself.
Now, you could claim that it is tedious to do for many widgets, but on the other hand, you could write a simple hash define or inline function to do this repetitive work for you, which would only result a one-liner call, basically, which you would need to use anyway when changing the state.

How can I put an image in a QTextEdit outside of a regular text line?

I would like to be able to insert an image into a QTextEdit above any given letter in a line of text.
Initially, I thought I would be able to use the position:absolute attribute in an html object, but QTextDocument doesn't support that. I've also tried inserting html images at the cursor, but the closest I can get with that approach is inserting an image in a text line which using html like this:
<img src="/smiley.png" style="position:absolute; left:200px; right:200px;" height="16" width="16"/>
results in something like this:
s:)miley
but I would want the smiley face to appear above the letter 'm' for example. (Sorry, can't post images)
This lead me to believe I would need to use the background-image attribute, but given that I am using a QTextEdit, I'm not sure what Qt object I would apply that to in order to get the image to appear only over the 'm'.
Does anyone have any tips for absolute positioning of images in QTextEdit?
EDIT
While that would put an image where the letter was, it would put the image in line with the text. Whereas I want the image above the actual text. Like this:
:)
smiley
Where the smile is above the text, but not on its own text line. Sort of in the space between lines.