在Qt中测量文本宽度 [英] Measuring text width in Qt

查看:673
本文介绍了在Qt中测量文本宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Qt框架,如何测量以给定字体/样式呈现的一段文本的宽度(以像素为单位)?

Using the Qt framework, how do I measure the width (in pixels) of a piece of text rendered with a given font/style?

推荐答案

您可以使用 QFontMetrics 类 - 请参阅< a href =http://qt-project.org/doc/qt-5/QFontMetrics.html#width =nofollow> width()方法,它可以给你给定的QString的宽度。

You can use QFontMetrics class - see the width() method which can give you the width of a given QString.

QFont myFont(fontName, fontSize);;
QString str("I wonder how wide this is?");

QFontMetrics fm(myFont);
int width=fm.width(str);

这篇关于在Qt中测量文本宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆