(超简单)完美解决QTextEdit以及QTextbrowser高度自适应

又得用到这个了,突然发现我好沙雕,为什么不用滚动条来确定,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);  
        	}  
	    }  
	} */
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值