Results 1 to 2 of 2

Thread: QTextBrowser height adjusted to content

  1. #1
    Join Date
    Feb 2011
    Location
    Poland
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default QTextBrowser height adjusted to content

    I have a class DetailsView derived from QScrollArea. The class member iDescBox which is of type QGroupBox contains QTextBrowser iDescription item within it.

    I wolud like to adjust height of iDescBox according to content of iDescription because I don't want to have vertical scrollbar in iDescBox.

    I have created method adjustDescription which is called from showEvent and resizeEvent of DetailsView, unfortunately it does not do what I wanted it to do.

    When I enter(make visible) DetailsView for the first time I can see vertical scrollbar in iDescBox. When I enter DetailsView second time iDescBox is adjusted correctly and when I enter DetailsView for the third time it has a horizontal scrollbar. After this secuence repeats

    Can anybody tell me what am I doing wrong, I attach source of adjustDescription method ?

    Qt Code:
    1. void DetailsView::adjustDescription()
    2. {
    3. if (iDescBox->isVisible())
    4. {
    5. QMargins margs = iDescBox->contentsMargins();
    6. int w = viewport()->width()
    7. - margs.left() - margs.right();
    8.  
    9. QTextDocument* doc = iDescription->document();
    10. doc->setTextWidth(w);
    11. doc->adjustSize();
    12.  
    13. int h = doc->size().toSize().height()
    14. + margs.bottom() + margs.top() + 27;
    15.  
    16. //If adjusted document size is bigger then viewport size and vertical
    17. //scrollbar is not displayed then adjust description one more time
    18. if ( iDescBox->y() + h > viewport()->height()
    19. && !verticalScrollBar()->isVisible())
    20. {
    21. w -= verticalScrollBar()->width();
    22.  
    23. doc->setTextWidth(w);
    24. doc->adjustSize();
    25.  
    26. h = doc->size().toSize().height()
    27. + margs.bottom() + margs.top() + 27;
    28. }
    29.  
    30. iDescBox->setMinimumHeight(h);
    31. iDescBox->setMaximumHeight(h);
    32. }
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2011
    Location
    Poland
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: QTextBrowser height adjusted to content

    Hmm... Method presented above wasn't best way to solve described problem...

    Below I present working adjustDescription method:

    Qt Code:
    1. void DetailsView::adjustDescription()
    2. {
    3. if (iDescBox->isVisible())
    4. {
    5. QMargins margs = iDescBox->layout()->contentsMargins();
    6. QMargins amargs = widget()->layout()->contentsMargins();
    7. QTextDocument* doc = iDescription->document();
    8.  
    9. qreal lineWidth = viewport()->width()
    10. - margs.left() - margs.right() - amargs.right()
    11. - amargs.left() - doc->documentMargin()*2;
    12.  
    13. doc->setPageSize(QSizeF(lineWidth,-1));
    14. int boxHeight = doc->documentLayout()->documentSize().toSize().height()
    15. + margs.bottom() + margs.top() + 50 + 4;
    16.  
    17. if ( iDescBox->y() + boxHeight > viewport()->height()
    18. && !verticalScrollBar()->isVisible())
    19. {
    20. lineWidth -= verticalScrollBar()->width();
    21. doc->setPageSize(QSizeF(lineWidth,-1));
    22. }
    23.  
    24. int newHeight = doc->documentLayout()->documentSize().toSize().height();
    25. boxHeight = newHeight + margs.bottom() + margs.top() + 50 + 4;
    26.  
    27. iDescription->viewport()->setMinimumSize(lineWidth,newHeight);
    28. iDescription->viewport()->setMaximumSize(lineWidth,newHeight);
    29.  
    30. iDescription->setMinimumSize(lineWidth, newHeight);
    31. iDescription->setMaximumSize(lineWidth, newHeight);
    32.  
    33. iDescBox->setMinimumSize(lineWidth + doc->documentMargin()*2,
    34. boxHeight);
    35. iDescBox->setMaximumSize(lineWidth + doc->documentMargin()*2,
    36. boxHeight);
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How QRectF::adjusted() works
    By onurozcelik in forum Newbie
    Replies: 1
    Last Post: 27th May 2010, 09:04
  2. height property not adjusted when QTreeView item selected
    By hubbobubbo in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2010, 15:38
  3. Loading content into QTextBrowser on demand
    By jon-ecm in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2009, 02:38
  4. Replies: 1
    Last Post: 21st September 2006, 10:37
  5. QTextBrowser help please!
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2006, 20:27

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.