PDA

View Full Version : QTextEdit - dynamic height



munna
24th March 2007, 08:01
Hi,

I have a QTextEdit with fixed width and wrap mode set to setLineWrapMode(QTextEdit::WidgetWidth);

Now, when user edits the text in this box, i need to increase the height of this QTextEdit so that the vertical scrollbar never appears and all the text entered could be seen.

I have connected a slot to the textChanged() signal but not sure how exactly can the height be calculated.

Any ideas on how I can implement this one?

Thanks a lot.

vermarajeev
24th March 2007, 08:25
Hi,

I have a QTextEdit with fixed width and wrap mode set to setLineWrapMode(QTextEdit::WidgetWidth);

Now, when user edits the text in this box, i need to increase the height of this QTextEdit so that the vertical scrollbar never appears and all the text entered could be seen.

I have connected a slot to the textChanged() signal but not sure how exactly can the height be calculated.

Any ideas on how I can implement this one?

Thanks a lot.


Use virtual int heightForWidth ( int w ) const . Give the width as argument and get the height. then use resize(int width, int height).

Something like this

void myClass::slotTextChanged()
{
int height = textEdit->heightForWidth(textEdit->width());
textEdit->resize(textEdit->width(), height);
}

munna
24th March 2007, 09:43
Use virtual int heightForWidth ( int w ) const . Give the width as argument and get the height. then use resize(int width, int height).

From the docs:



int QWidget::heightForWidth ( int w ) const [virtual]
Returns the preferred height for this widget, given the width w.
If this widget has a layout, the default implementation returns the layout's preferred height. if there is no layout, the default implementation returns -1 indicating that the preferred height does not depend on the width.


My widget is not part of any layout and therefore I am getting -1 which I cannot use.

Isn't there a way by which we can find the number of lines that the text are occupying and then based on the font we can calculate the approximate height ?

Thanks a lot for your help

vermarajeev
26th March 2007, 05:03
From the docs:



My widget is not part of any layout and therefore I am getting -1 which I cannot use.

Isn't there a way by which we can find the number of lines that the text are occupying and then based on the font we can calculate the approximate height ?

Thanks a lot for your help

If heightforwidth doesnt work then you will have to write your own logic to implement the same. If the font size also changes dynamically then you will have tough time. If the font size is same then you can write something like this

Check if the noOfLines has been changed, if yes then resize the textEdit with textEditHeight + fontHeight. If not decrease the height.

Hope this helps