Results 1 to 5 of 5

Thread: QTextEdit auto resize

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QTextEdit auto resize

    Hey there,

    I'm displaying some text into a read only QTextEdit.

    Is there a way to set the widget to Auto resize depending on the length of text ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit auto resize

    If you reimplement sizeHint() to take into consideration the size of the document and call updateGeometry() whenever the document content changes then yes.

  3. The following user says thank you to wysota for this useful post:

    bunjee (26th October 2007)

  4. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextEdit auto resize

    Okay here is a first implementation which doesn't seem to work as expected :

    Qt Code:
    1. ZeParagraphWidget::ZeParagraphWidget(const QString & string,
    2. QWidget *parent) :
    3. QTextEdit("", parent)
    4. {
    5. // Size policy
    6. QSizePolicy localSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    7. setSizePolicy(localSizePolicy);
    8.  
    9. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    10. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    11. }
    12.  
    13. void ZeParagraphWidget::setText(const QString & text)
    14. {
    15. ZeLog::get()->Print("ZeParagraphWidget - setText\n");
    16.  
    17. QTextEdit::setText(text);
    18.  
    19. updateGeometry();
    20. }
    21.  
    22. /* virtual */ QSize ZeParagraphWidget::sizeHint() const
    23. {
    24. QSize sizeRect(document()->idealWidth(), document()->size().height());
    25.  
    26. return sizeRect;
    27. }
    To copy to clipboard, switch view to plain text mode 
    By default the box is way to large, and the width doesn't fit the text.
    I'm not sure about the size policy too.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit auto resize

    No, it won't work that way. First of all you have to include the widget frame in your calculations. And second of all, if you calculate a width of a document, you must set it first before calling size().height(). Or don't use idealWidth (because it may return a value smaller than you need) just return document()->size() extended by the size of the widget decoration.

  6. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextEdit auto resize

    The following is working good in my case:

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. /* virtual */ int ZeParagraphWidget::heightForWidth(int w) const
    5. {
    6. ZeLog::get()->Print("ZeParagraphWidget - heightForWidth %d %f\n", w, document()->size().height());
    7.  
    8. return (int) (document()->size().height());
    9. }
    10.  
    11. //=============================================================================
    12. //=============================================================================
    13. // Slots
    14. //=============================================================================
    15. //=============================================================================
    16.  
    17. void ZeParagraphWidget::onContentsChanged()
    18. {
    19. ZeLog::get()->Print("ZeParagraphWidget - onContentsChanged\n");
    20.  
    21. updateGeometry();
    22. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Auto resize QLabel
    By munna in forum Newbie
    Replies: 12
    Last Post: 6th August 2008, 14:39
  2. QTextEdit + auto scroll
    By jbpvr in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 10:30
  3. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07:03
  4. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 01:32
  5. How to get a QDockWidget to auto resize?
    By bitChanger in forum Qt Programming
    Replies: 4
    Last Post: 6th January 2006, 17:10

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.