Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTextEdit::adjustSize() not working?

Tags:

qt

qtextedit

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: Expanding
Vertical Policy : MinimumExpanding
minimumSize : 2×22
maximum Size : 300×100
lineWrapMode: WidgetWidth

like image 751
Noob Avatar asked Nov 04 '22 08:11

Noob


1 Answers

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.

  1. You can use QFontMetrics to calculate width of every word in your text
  2. You can set height to 22 and increment it until maximumHeight hitted or vertical scrollbar dissapears.
  3. You can get some info from sources of QTextEdit itself and subclass it, reimplementing something (adjustSize()?) there.
like image 107
Dmitry Melnikov Avatar answered Nov 15 '22 08:11

Dmitry Melnikov