方法 1 ui->textEdit->document()->adjustSize();
QTextDocument *document = ui->textEdit->document();
ui->textEdit->setFixedHeight(document->size().height()+20);
方法2 ui->textEdit->setPlainText(string);
ui->textEdit->document()->adjustSize();//将文档调整到合理的大小。
int newheight = ui->textEdit->document()->size().rheight();
if (newheight != ui->textEdit->height())
{
ui->textEdit->setFixedHeight(newheight+10);
}