QTextEdit::adjustSize() not working? - qt

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.

Related

How to delete the space between QLabel and QLineEdit

I have a QDialog window like this and I want to delete the space between 'Length', 'n', 'm' and corresponding QLineEdit input boxes. How can I achieve that ?
If you use gridlayout, I am not sure why your output looks like that. Generally Qt will not leave huge empty space like that, There are three possibility I can think of:
You have many SPACE after Length:, M: or N:
The layoutHorizontalSpacing is too large in your grid manager.
The layoutColumnStretch was set to in favor of label in your grid manager, should be "0,0", not "1,0". I mean, stretch of Label should not be higher than lineedit.
Still, I would use a simple form layout in your application.
All you need to do is reset the alignment of the labels:
label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlighVCenter)
This can also be done in via the Property Editor in Qt Designer.
You have to decide what to do with the space you want to remove :
Reduce the whole widget : it depends on the surrounding layout, adding an horizontal layout on right or left of it will put the space out of your widget.
Space on the left of your labels : you can align the label text to the right as ekhumoro suggested, or directly reduce and align to the right the whole frame by adding an horizontal spacer on its left (in the surrounding layout).
Space on the right of your line-edits : like above you can add horizontal spacers or reduce and align the frame.
Expand the line-edits : remove their fixed width (default horizontal policy is expanding) or set a bigger one.
The point is : the space has to be somewhere except if you reduce the parent widget or enlarge some the inner widgets. Size-policy is useful to tell which widget should take the available space, spacers are useful to let empty space between widgets.

QTextEdit display width vs text width

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());

Enable QLabel to shrink even if it truncates text

How can I get a QLabel to be resized even if it means truncating its containing text? I have a QLabel stretching the whole horizontal space of a Widget. When setting its text I make sure it is correctly truncated, ie getting its FontMetrics and Width and using metrics.elidedText().
But when the user resizes the widget the Label doesn't allow it to shrink any further since it would truncate its text.
Any ideas how to solve this? The simplest solution I think would be to somehow tell the QLabel to always shrink and then catch the resize event and correctly format the text - I just have no idea how to do the first part (different size policies don't help)
Although you mention that setting size policies didn't help, setting the QLabel's horizontal size policy to QSizePolicy::Ignored should tell the containing layout manager to ignore any minimum size hint from the label. An alternative would be to set the QLabel's minimum horizontal size to a non-zero value, like 1. If neither of these work then there is something else interfering.

Flex: Determining number of lines a spark text area can hold without overflowing

I want to implement paging in a spark text area. For that I want to find out the number of lines a spark textArea can hold before the scrollbars appear and just feed that much lines to the text area.
http://blog.flexexamples.com/2010/01/13/determining-the-number-of-lines-in-a-spark-richeditabletext-control-in-flex-4/
The number of lines a TextArea can hold before a vertical ScrollBar appear is determinated by the heightInLines property.
The code
var number_of_lines:int=textArea.heightInLines;
will return inside the variable number_of_lines the number of lines you are looking for, and textArea is the id of the TextArea object you are checking.
var numLines:int=t.heightInLines;
var numChars:int=t.widthInChars;
The documentation:
spark.components.TextArea.heightInLines():Number The default height of
the control, measured in lines. The control's formatting styles, such
as fontSize and lineHeight, are used to calculate the line height in
pixels.
You would, for example, set this property to 5 if you want the height
of the RichEditableText to be sufficient to display five lines of
text.
If this property is NaN (the default), then the component's default
height will be determined from the text to be displayed.
This property will be ignored if you specify an explicit height, a
percent height, or both top and bottom constraints.
RichEditableText's measure() method uses widthInChars and
heightInLines to determine the measuredWidth and measuredHeight. These
are similar to the cols and rows of an HTML TextArea.
Since both widthInChars and heightInLines default to NaN,
RichTextEditable "autosizes" by default: it starts out very small if
it has no text, grows in width as you type, and grows in height when
you press Enter to start a new line.

QLabel: interlinea/linespacing in WordWrap mode

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.

Resources