Qt QLabel HTML font size fails badly - c++

In my Qt application (uses Qt 4.7.0 from Ubuntu 10.10 Linux repository) i tried to use Qt::RichText QLabels using the following HTML:
label_1->setText("<font size=64>size=64</font>");
label_2->setText("<font color=red size=10>size=10</font>");
label_3->setText("<font color=blue size=14>size=14</font>");
For some reason the font sizes are not set properly. All the widgets get the same font size, one that is larger than the default one but still the wrong one. The font size set for the first widget seems to influence the size that the following widgets will use. Setting only the color attribute leaves the label text in its standard size.
I also tried to reproduce this in the QtDesigner and the same problem happens there.
Setting the text format to Qt::Richtext does not have any effect. And using quotes around the HTML attribute values does not change anything either.
What am I missing?

Okay, so font size is supported but have you tried CSS-style font-size:64pt? CSS is better because the size has explicit units.

Related

qt on Mac: Form Layout fails to stretch horizontally

I'm still trying to do some pretty basic stuff with qt, and it's been a struggle. I'm specifically targeting for Mac. My current problem is getting the forms to not suck.
The simple problem. Create a new MainWindow app. Go into the Qt Creator (open Forms -> mainwindow.ui).
Drag 3 labels into place. Then I dragged 2 Line Edits and for fun, a Dial, but that part probably doesn't matter.
Click the main window and then tell it to use Form Layout. Within Qt Creator if I resize the window, my various line edits expand to fill the available space, exactly like I want.
Run the app. All the line edit fields are a fixed length, very short (if I don't override their minimum width), and do NOT resize as I resize the window.
Three quarters of the reason to use Form Layout is for resizing capability.
I've tried clicking on the central widget before setting to Form Layout: no change in behavior.
I do get reasonable behavior if I use a grid layout, although I have to add a vertical spacer at the bottom or my dial resizes crazily as I play with the window size.
So... Am I just doing something wrong with Form Layout? Or does it not work well on Mac and I should use Grid Layout instead?
I really miss Motif's XmFormLayout. It took time to set all the constraints, but I could make my forms do exactly what I wanted. Ah, but so 1990s.
On the panel to the right where you can see the objects, click the centralWidget. In the property panel, scroll down until you get to the layout properties of the form layout. Set the layoutFieldGrowthPolicy to ExpandingFieldsGrow.
By default, the line edits are not growing to fill the available space on macOS because they are not supposed to. This is some sort of macOS UI guideline. QFormLayout follows the platform conventions by default.
It would also be nice if I can figure out how to NOT necessarily expand everything to full width. I have a few Line Edit widgets that I'd like to keep short. They're going to hold TCP port numbers.
For those, set the Horizontal Policy property (it's grouped inside the sizePolicy property group in the properties panel) of the affected line edits to something suitable. In this case, probably to Fixed, but look up the documentation first of what each setting does.

Qt draws text with otf font outside the element

Today I switched the font of my application from one of the standard system fonts to Sinkin in the form of an otf file, I've added to my resource.qrc.
The following code sets the default font of the application object.
int id = QFontDatabase::addApplicationFont(":/fonts/font.otf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
qapp.setFont(family);
QFont font = qapp.font();
font.setPointSize(opt_font_size);
qapp.setFont(font);
Now it seems, that the font is placed a bit too high in all elements. That means buttons, labels, input fields and tabs. Or Qt does not realize, that it has to adjust the size of the elements to fit the new font.
Changing the font size via setPointSize does not fix it. Everything just gets smaller or bigger.
Any ideas where I can dig into?

How to properly display unicode symbols on Linux using QT?

I want to add an Unicode Symbol as a list widget item in QT. (Particularly this item : http://www.fileformat.info/info/unicode/char/25B6/index.htm )
I'm using following method for this :
this->addItem( new QListWidgetItem( QString::fromUtf8("\u25B6")) ) ;
But when I open the widget I see only a blank rectangle in place of this unicode symbol. I even tried other unicode symbols too, but they too are showing only blank rectangle.
What's wrong in this method?
EDIT: After following the answer, I changed the font of QListWidgetItem to Serif and it worked.
There are several possibilities, but the most probable one is that the font you are using doesn't implement the glyph at all. Can you see the triangle in eg. an editor when using the same font as your Qt font?
Edit: The fonts (which are stored in the system's font files) are the commands to draw the images of each character on the screen). Many (if not all) fonts are incomplete, which means they are not able to represent all 2,000,000,000+ codes which are possible in the unicode (the numbers which represent the characters). The files would just be to large to be practical.
The triangles you want printed are fairly basic, and should be available in many font sets. Liberation Sans and Liberation Serif are two I just checked.
I suspect Qt uses the font set of the system, which can probably be changed in the System Settings somewhere. If you tell us which distribution you are using (i.e. Ubuntu, Debian, ...), maybe we can help.

Set fixed size of a QDialog without cutting text

I currently set a QDialog to have a fixed size using the following code
dlg->setWindowModality(Qt::WindowModal);
dlg->setFixedSize(dlg->size());
Now as a result of this code whatever size I save my ui form in QT Designer. It sticks to that size.This however ends up being a issue in some systems and displays. Where my Qlabels begin to cut from the sides due to the shortage of space.I wanted to know what would be the proper way of doing this ? How would I determine which size would accommodate the layout on the form.The form itself has a horizontal layout which has multiple layouts in it ??
Try
dlg->adjustSize();
dlg->setFixedSize(dlg->sizeHint());

Auto Sizing Labels in Qt

I'm developing a Qt application and it's currently in an internal beta test. One member of the company has Windows configured to display text larger than its normal size, which breaks my UI. The About page, for example, currently looks like this:
but under his settings, looks like this (note the clipped text):
Coming from a C#/Winforms background, I'm amazed that I can't seem to find some easily configurable label property such as Form.AutoSize that will automatically size the labels to fit their containing text. I've tried messing with sizePolicy, scaledContents, and a few other properties, but none seem to do this.
I've come across various threads (such as this one) which give instructions for scaling the text to the label, but I want to do the opposite - scale the label to the text to facilitate for those with enlarged text settings like my co-worker. Is there a straightforward way to do this?
There are at least three solutions to this problem.
Use layouts. Their contents are scaled according to the size of the window.
Make a code which is executed whenever window size is changed. In that code, you get the width of the longest text in the window (How?)(another way) and then set window wider than that.
Do the same as in solution #2, but execute the code only when the dialog is shown. After that, alter the window properties so that its size cannot be changed.