又得用到这个了,突然发现我好沙雕,为什么不用滚动条来确定,emmmmm,突然就变简单了
int height=textBrowser->verticalScrollBar()->maximum()-textBrowser->verticalScrollBar()->minimum()+textBrowser->verticalScrollBar()->pageStep();
textBrowser->setFixedHeight(height);
//可以使用信号与槽 见文章末尾
好了
下面是原来弄的,也可以
找了半天,终于解决了QTextbrowser高度自适应问题,一段代码,我需要的仅是以下代码,仅参考,若要使用,请使用文章后面的代码,利用信号与槽,完美解决。
//实践证明我觉得下面这句不要
ui->textBrowser->document()->adjustSize();//将文档调整到合理的大小。
int newheight = ui->textBrowser->document()->size().rheight();
if (newheight != ui->textBrowser->height())
{
ui->textBrowser->setFixedHeight(newheight);
}
原博客地址:https://blog.youkuaiyun.com/qiangzi4646/article/details/100524886
QTextBrowser*textBrowser= new QTextBrowser(this);
...
connect(textBrowser->document(),SIGNAL(contentsChanged()),this,SLOT(textAreaChanged()));
...
private slots:
void textAreaChanged()
{
int height=textBrowser->verticalScrollBar()->maximum()-textBrowser->verticalScrollBar()->minimum()+textBrowser->verticalScrollBar()->pageStep();
textBrowser->setFixedHeight(height);
/*之前的
QTextDocument *document=qobject_cast<QTextDocument*>(sender());
document->adjustSize();
if(document)
{
QTextEdit *editor=qobject_cast<QTextEdit*>(document->parent()->parent());
if (editor)
{
int newheight = document->size().rheight()+10;
if (newheight != editor->height())
{
editor->setFixedHeight(newheight);
}
}
} */
}