Results 1 to 4 of 4

Thread: QTextEdit - dynamic height

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QTextEdit - dynamic height

    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.

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - dynamic height

    Quote Originally Posted by munna View Post
    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
    Qt Code:
    1. void myClass::slotTextChanged()
    2. {
    3. int height = textEdit->heightForWidth(textEdit->width());
    4. textEdit->resize(textEdit->width(), height);
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - dynamic height

    Quote Originally Posted by vermarajeev
    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

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - dynamic height

    Quote Originally Posted by munna View Post
    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

Similar Threads

  1. How can I get the Systray height ?
    By Nyphel in forum Qt Programming
    Replies: 3
    Last Post: 12th March 2007, 09:04
  2. QTextEdit height using Qt3.3.5
    By vermarajeev in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2006, 08:43
  3. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  4. height()
    By mickey in forum Newbie
    Replies: 1
    Last Post: 22nd March 2006, 20:32
  5. Painting to QTextEdit
    By gesslar in forum Qt Programming
    Replies: 8
    Last Post: 18th February 2006, 18:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.