简体   繁体   English

QTextEdit显示宽度与文本宽度

[英]QTextEdit display width vs text width

I'm creating a 'scrolling-text' class in Qt, using a QTextEdit (read-only, no scrollbars, moveCursor) and a QTimer - simple and working. 我在Qt中创建了一个“滚动文本”类,使用QTextEdit (只读,无滚动条,moveCursor)和QTimer - 简单而有效。

My problem is when the text sent to the class is shorter (narrower) than the QTextEdit-box. 我的问题是发送到类的文本比QTextEdit框更短(更窄)。 Silly, I agree, but, being new to Qt, I didn't find an easy way to compare the width of the given text (depending on the font) and the actual width that can be displayed inside the QTextEdit (after calculating the FrameStyle, etc.). 傻,我同意,但是,作为Qt的新手,我没有找到一种简单的方法来比较给定文本的宽度(取决于字体)和可以在QTextEdit内显示的实际宽度(在计算FrameStyle之后)等)。 I presume I need to calculate the pixels. 我认为我需要计算像素。

Any ideas? 有任何想法吗? Thanks 谢谢

您可以使用QFontMetrics获取文本的宽度:

int textWidth = myTextEdit->fontMetrics().width(myTextEdit->text());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM