Noob
Noob

Reputation: 217

QTextEdit::adjustSize() not working?

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

Upvotes: 5

Views: 1199

Answers (1)

Dmitry Melnikov
Dmitry Melnikov

Reputation: 743

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.

Upvotes: 2

Related Questions