Ton*_*nyK 13
如果你使用QPlainTextEdit
,这样的东西应该做的伎俩:
void SetHeight (QPlainTextEdit* edit, int nRows)
{
QFontMetrics m (edit -> font()) ;
int RowHeight = m.lineSpacing() ;
edit -> setFixedHeight (nRows * RowHeight) ;
}
Run Code Online (Sandbox Code Playgroud)
您可能希望添加两个或三个像素作为边距; 实验会说明.
小智 6
改进关于 的公认答案QPlainTextEdit
。除了 之外lineSpacing
,值setFixedHeight
还应包含:底层的 2 个边距、QTextDocument
框架的 2 个宽度和小部件的内容边距。除此之外,QFontMetrics
必须从文档的字体中获取,而不是小部件本身的字体。因此,假设函数setHeight
应如下所示:
void setHeight (QPlainTextEdit *ptxt, int nRows)
{
QTextDocument *pdoc = ptxt->document ();
QFontMetrics fm (pdoc->defaultFont ());
QMargins margins = ptxt->contentsMargins ();
int nHeight = fm.lineSpacing () * nRows +
(pdoc->documentMargin () + ptxt->frameWidth ()) * 2 +
margins.top () + margins.bottom ();
ptxt->setFixedHeight (nHeight);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16115 次 |
最近记录: |