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