我使用QPlainTextEdit控件并自动换行,我想知道如何更新行计数器,当文本块被包装时(如果要递增的行数。)
基础QTextDocument has a signal to detect when the block count changes,但不是相应的行计数更改。
是否可以检测QTextDocument的自动换行和行数增加?
答案 0 :(得分:0)
我使用 QAbstractTextDocument 的信号documentSizeChanged解决了:
void QAbstractTextDocumentLayout :: documentSizeChanged(const QSizeF& newSize)[信号]
此信号在大小时发出 文档布局更改为newSize。的子类 QAbstractTextDocumentLayout应该在发出此信号时发出 文档的整个布局大小发生了变化。这个信号很有用 显示文本文档的小部件,因为它使它们能够更新 他们的滚动条正确。
我知道我的字体大小,并且获得新底层文档的精确大小,这样我就可以计算文本文档的行数(包裹与否)。
答案 1 :(得分:0)
有点晚了但也许我的回答可以帮助别人 我的公司几乎有同样的需求,我解决了这个问题:
// This example show how to get the visual number of lines
QPlainTextEdit *pte = new QPlainTextEdit();
pte->setAttribute(Qt::WA_DontShowOnScreen);
pte->show();
pte->setFixedWidth(50);
pte->setPlainText("Hello world!");
/* The next line return the number of necessary line
to display the text "Hello World!" with width of 50px */
int lineCount = pte->document()->documentLayout()->documentSize().height();
祝你好运