如何设置QTextEdit的行数?

I use a QTextEdit for some inputs. But I want to adjust the height of the box. Can I set the height based on the number of lines I want to have visible at a time?

#0

If you use QPlainTextEdit, something like this should do the trick:

void SetHeight (QPlainTextEdit* edit, int nRows)
  {
  QFontMetrics m (edit -> font()) ;
  int RowHeight = m.lineSpacing() ;
  edit -> setFixedHeight  (nRows * RowHeight) ;
  }

You might want to add two or three pixels as margin; experiment will tell.

#1

Improving the accepted answer about QPlainTextEdit. In addition to lineSpacing, value for setFixedHeight should contain: 2 margins of the underlying QTextDocument, 2 widths of the frame and widget's contents margins. Besides that, QFontMetrics must be got from a font of the document, not of the widget itself. So, hypothetical function setHeight should read as follows:

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);
}

#2

Use QFont to determine the height of a single line of text in the QTextEdit (QTextEdit should have a font property). After that multiply the QFont's height value with the number of lines you want to show and set the widget's (minimum-)height to that value.

#3

QTextEdit is a normal widget, so you can use minimumHeight property. I believe, however, that it is really impossible to set minimum height based on number of lines. This would resize automagically the minimum size of a widget every time you change size of the font. But if you know the size of the font, you can set some usable minimum size of your widget.

#4

This should work:

QTextEdit *myEdit = new QTextEdit(myContentString);
QSize myEditSize = myEdit->document()->size().toSize();
myEditSize.setWidth(QWIDGETSIZE_MAX);
myEdit->setMaximumSize(myEditSize);

推荐文章

jQuery链接不可单击,但我可以右键单击“在新选项卡中打开”?

jQuery链接不可单击,但我可以右键单击“在新选项卡中打开”?

推荐文章

NSXMLParser抛出EXC_BAD_访问

NSXMLParser抛出EXC_BAD_访问

推荐文章

在表格视图中的单元格中显示阴影

在表格视图中的单元格中显示阴影

推荐文章

如何在更新父实体时删除旧的子记录

如何在更新父实体时删除旧的子记录

推荐文章

使用AWT面板的静态嵌套类下拉列表

使用AWT面板的静态嵌套类下拉列表

推荐文章

正在寻找OpenGL ES api来测试对象是否太小而无法被相机看到?

正在寻找OpenGL ES api来测试对象是否太小而无法被相机看到?

推荐文章

是否有方法获取在同一网络中不同web服务器上运行的所有coldfusion计划任务的列表?

是否有方法获取在同一网络中不同web服务器上运行的所有coldfusion计划任务的列表?

推荐文章

POST值不在javascripts中显示

POST值不在javascripts中显示

推荐文章

在Linux上编译的程序没有在ifdef部分获得代码

在Linux上编译的程序没有在ifdef部分获得代码

推荐文章

如何在MacOS上轻松打开lua文件?

如何在MacOS上轻松打开lua文件?

推荐文章

使用NUnit在Silverlight 4应用程序中对WCF RIA服务进行单元测试

使用NUnit在Silverlight 4应用程序中对WCF RIA服务进行单元测试

推荐文章

需要重新提交的Iphone应用程序更改

需要重新提交的Iphone应用程序更改

推荐文章

定义路径

定义路径

推荐文章

执行时绑定插入“失败”

执行时绑定插入“失败”

推荐文章

在设计时用抽象基类呈现用户控件

在设计时用抽象基类呈现用户控件

推荐文章

PHP中的Get和set(private)属性与C中的一样,不使用getter setter magic方法重载

PHP中的Get和set(private)属性与C中的一样,不使用getter setter magic方法重载