PDA

View Full Version : QTextBrowser height adjusted to content



michalk
20th March 2011, 13:07
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 ?



void DetailsView::adjustDescription()
{
if (iDescBox->isVisible())
{
QMargins margs = iDescBox->contentsMargins();
int w = viewport()->width()
- margs.left() - margs.right();

QTextDocument* doc = iDescription->document();
doc->setTextWidth(w);
doc->adjustSize();

int h = doc->size().toSize().height()
+ margs.bottom() + margs.top() + 27;

//If adjusted document size is bigger then viewport size and vertical
//scrollbar is not displayed then adjust description one more time
if ( iDescBox->y() + h > viewport()->height()
&& !verticalScrollBar()->isVisible())
{
w -= verticalScrollBar()->width();

doc->setTextWidth(w);
doc->adjustSize();

h = doc->size().toSize().height()
+ margs.bottom() + margs.top() + 27;
}

iDescBox->setMinimumHeight(h);
iDescBox->setMaximumHeight(h);
}

}

michalk
21st March 2011, 01:08
Hmm... Method presented above wasn't best way to solve described problem... :o

Below I present working adjustDescription method:



void DetailsView::adjustDescription()
{
if (iDescBox->isVisible())
{
QMargins margs = iDescBox->layout()->contentsMargins();
QMargins amargs = widget()->layout()->contentsMargins();
QTextDocument* doc = iDescription->document();

qreal lineWidth = viewport()->width()
- margs.left() - margs.right() - amargs.right()
- amargs.left() - doc->documentMargin()*2;

doc->setPageSize(QSizeF(lineWidth,-1));
int boxHeight = doc->documentLayout()->documentSize().toSize().height()
+ margs.bottom() + margs.top() + 50 + 4;

if ( iDescBox->y() + boxHeight > viewport()->height()
&& !verticalScrollBar()->isVisible())
{
lineWidth -= verticalScrollBar()->width();
doc->setPageSize(QSizeF(lineWidth,-1));
}

int newHeight = doc->documentLayout()->documentSize().toSize().height();
boxHeight = newHeight + margs.bottom() + margs.top() + 50 + 4;

iDescription->viewport()->setMinimumSize(lineWidth,newHeight);
iDescription->viewport()->setMaximumSize(lineWidth,newHeight);

iDescription->setMinimumSize(lineWidth, newHeight);
iDescription->setMaximumSize(lineWidth, newHeight);

iDescBox->setMinimumSize(lineWidth + doc->documentMargin()*2,
boxHeight);
iDescBox->setMaximumSize(lineWidth + doc->documentMargin()*2,
boxHeight);
}
}