How to set number of lines for an QTextEdit?

Mnementh picture Mnementh · Mar 10, 2011 · Viewed 19.9k times · Source

I use a QTextEdit for some inputs. But I want to adjust the height of the box. Can I set the height based on the number of lines I want to have visible at a time?

Answer

TonyK picture TonyK · Mar 10, 2011

If you use QPlainTextEdit, something like this should do the trick:

void SetHeight (QPlainTextEdit* edit, int nRows)
  {
  QFontMetrics m (edit -> font()) ;
  int RowHeight = m.lineSpacing() ;
  edit -> setFixedHeight  (nRows * RowHeight) ;
  }

You might want to add two or three pixels as margin; experiment will tell.