How do I set line height in QLabel when in WordWrap mode?
Use HTML text:
QString template = "<p style=\"line-height:%1%\">%2<p>";
QString targetText = template.arg(myPercentage).arg(myTex);
QLabel *l = new QLabel(targetText, this);
where myPercentage is like 60 - 80.
You will get condensed lines in the WordWrap mode
There is no line spacing property in QLabel. You can change the widget font, which will change the line's height, but I suspect that is not what you want.
Line height is computed from the QFont of the widget and can be obtained by the QFontMetrics associated with the widget. Using this information, you may create your own widget that has a line spacing property (and a text wrap mode), but that represents a lot of low-level work.
You can also edit the HTML of the QLabel directly in Qt Designer.
Select the label in Qt Designer.
In the Property Editor, under the QLabel section, select the text property and press the ... button.
Select the "source" tab and edit the HTML from there.
Here are two examples that control the line spacing of a QLabel using HTML (tested in Qt 5.7). I am sure there are many more (and some better) ways to write the HTML, but this should be a good start.
Example 1
<html><head/><body>
<p style="line-height:120"><span>
This is the first line of the label.<br>
This is the second line.<br>
This is the third and final line.
</span></p>
</body></html>
This example is neater if the line spacing is the same for the whole paragraph.
Example 2
<html><head/><body>
<p style="line-height:20"><span>This is the first line of the label.</span></p>
<p style="line-height:20"><span>This is the second line.</span></p>
<p style="line-height:100"><span>This is the third and final line.</span></p>
</body></html>
This example allows you to control the spacing of each line individually. I had to make the height of the last line 100 to prevent Qt from cutting it in half. I assume it affects how Qt calculates the height of the label as a widget.
Related
Im changing text of QTabWidget dynamically and also changing its font family. so the text in the tabBar header is exceeding the actual width. how i can control the text to wrap inside the tabBar width like
"Text.."
im setting the font family using stylesheet
QString menuBarStyle = QString("QTabBar::tab {font-family: %1;font-style: %2;font-size: 11pt;font-weight: %3;width: 130px}").arg(m_currentFont.family()).arg(fontStyle).arg(fontWeight);
TabWidget->setStyleSheet(menuBarStyle);
Qt stylesheet technique may or may not let to solve it that but I once resolved similar problem like that:
QString elidedText(const QFont& someFont, int width)
{
QFontMetrics metrix( someFont );
return metrix.elidedText(text, Qt::ElideRight, width);
}
So, in addition to font object you have to have the width to fit the text in. Also frequently applying changes via the stylesheet for the widget can hardly be optimal solution, it does a bit too much to get the small change.
I want to paint controls directly into TextEdit. It is easy, but the problem is i need to have space for them, so they are not painted over the text.
For example (the whole line is representing what will be shown in TextEdit and the highlighted code should be qml component):
Here is button: button and here is text again.
I need somehow to reserve space for the button between Here is button: and and here is text again.
It looks like Qt qml doesn't provide any way how to specify font metrics (in that case i could ask the component what it's width is and just add
one whitespace char with properly setup font and its metrics and than
specify component coordinates so it is painted exactly where the space is).
I did it in Java SWT, because SWT StyledText allows to setup metrics for each character. So this is example how it should look.
You can use HTML markup in TextEdit
TextEdit {
textFormat: TextEdit.RichText
text: "<font size=50>text</font>"
}
Setting text for QTextEdit:
te->setPlainText(“Something”) ;
te->adjustSize();
should wrap around “Something” only, instead the QTextEdit is expanding to its maximum Width-Height, can’t fix it..
When I select “Something” on run time, only “Something” is highlighted, no added extra white spaces.
Expectations: when text is small enough to fit on one Line, the text edit shouldn’t expand in height, when the text needs to wrap, only the extra line width should be added not the maximum width.
if adjustSize(); is not called, the text will wrap on the width that was set in the .ui in the Creator, won't dynamically expand horizontally nor vertically..
Some Info:
Horizontal Policy: ExpandingVertical Policy : MinimumExpanding
minimumSize : 2×22maximum Size : 300×100lineWrapMode:
WidgetWidth
Yes, looks like there is no easy way to count lines in QTextEdit.
adjustSize() is made for QWidget and is not reimplemented for QTextEdit, it is based on sizeHint().
You can use your own method to count lines, f.e.
You can use QFontMetrics to calculate width of every word in your text
You can set height to 22 and increment it until maximumHeight hitted or vertical scrollbar dissapears.
You can get some info from sources of QTextEdit itself and subclass it, reimplementing something (adjustSize()?) there.
For example, we have a QLabel with MaximumWidth set to 400.
When we try to display some text with pixel width more than 400, it's shown cut off.
Is there any way to make QLabel display this string in multiple lines without using QFontMetrics or the like?
If I understood your question correctly, you should use the setWordWrap function for your label, with true as its parameter.
QLabel lbl("long long string");
lbl.setWordWrap(true);
In order to show multiple lines in QLabel, right click on QLabel and select 'change rich text'. This brings up dialog where you can type the text as you want to see including enter key. Setting the word wrap is not required for this.
If you set the word wrap as well (in QLabel properties) than it will wrap each individual line in the Qlabel if it was longer than the real estate.
As another option to wrap text using Qt Designer, you can check the box under Property Editor for a QLabel:
I'm creating a 'scrolling-text' class in Qt, using a QTextEdit (read-only, no scrollbars, moveCursor) and a QTimer - simple and working.
My problem is when the text sent to the class is shorter (narrower) than the QTextEdit-box.
Silly, I agree, but, being new to Qt, I didn't find an easy way to compare the width of the given text (depending on the font) and the actual width that can be displayed inside the QTextEdit (after calculating the FrameStyle, etc.). I presume I need to calculate the pixels.
Any ideas?
Thanks
You can get the width of a text using QFontMetrics:
int textWidth = myTextEdit->fontMetrics().width(myTextEdit->text());