×
Namespaces

Variants
Actions
(Difference between revisions)

How to get maximum Font Size in Qt to fit text into Widget

From Nokia Developer Wiki
Jump to: navigation, search
Avis (Talk | contribs)
(Created page with 'Category:QtCategory:Qt for MaemoCategory:Qt for Symbian {{CodeSnippet |id= |platform= All Qt Supported |devices= Emulator / desktop / device |category=Qt |subcategor…')
 
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Qt]][[Category:Qt for Maemo]][[Category:Qt for Symbian]]
+
[[Category:Qt]][[Category:UI]]
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=
+
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
|platform= All Qt Supported
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= Emulator / desktop / device
 
|devices= Emulator / desktop / device
|category=Qt
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|subcategory=Qt Gui
+
|platform= All Qt Supported
|creationdate=30 November 2010
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= QWidget, QFont, QFontMetrics
 
|keywords= QWidget, QFont, QFontMetrics
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 30 November 2010
 +
|author= [[User:Avis]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= UI
 
}}
 
}}
  
We can simply get text size  when we know Font attributes - just use the '''QFont''' and '''QFontMetrics''' like in this article [[CS001349_-_Calculating_text_width_in_Qt]]
 
  
And here is contrary task: set Font Size to maximally fit into widget's size. Simple loop will help.
+
== Basic Idea ==
  
<code cpp>
+
{{Abstract|visible=true|This article shows how you can calculate the maximum font size of text in a {{Qapiname|QWidget}} using the font attributes {{Qapiname|QFont}} and {{Qapiname|QFontMetrics}}. See also [[Archived:Calculating text width in Qt]] }}
 +
 
 +
 
 +
And here is contrary task: set font size to maximally fit into widget's size. Simple loop will help.
 +
 
 +
<code cpp-qt>
 
// Function gets aFont with initialized Font Family, and aText - which size is calculated
 
// Function gets aFont with initialized Font Family, and aText - which size is calculated
 
void TextWidget::calcFontSize(const QString aText, QFont& aFont)
 
void TextWidget::calcFontSize(const QString aText, QFont& aFont)
Line 47: Line 66:
 
</code>
 
</code>
  
--[[User:Avis|Avis]] 8:29, 30 November 2010 (UTC)
+
--[[User:Avis|Avis]] 8:29, 30 November 2010 (UTC)[[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:17, 11 October 2012

Article Metadata
Tested with
Devices(s): Emulator / desktop / device
Compatibility
Platform(s): All Qt Supported
Symbian
Article
Keywords: QWidget, QFont, QFontMetrics
Created: Avis (30 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)


[edit] Basic Idea

This article shows how you can calculate the maximum font size of text in a QWidget using the font attributes QFont and QFontMetrics. See also Archived:Calculating text width in Qt


And here is contrary task: set font size to maximally fit into widget's size. Simple loop will help.

// Function gets aFont with initialized Font Family, and aText - which size is calculated
void TextWidget::calcFontSize(const QString aText, QFont& aFont)
{
// get widget's width
int wd_width = size().width();
// get widget's height
int wd_hgt = size().height();
int fontPointSize;
// calculating loop
for (int i = 0; i < 200; i++)
{
aFont.setPointSize(i);
// create FontMetrics for resized font
QFontMetrics fm(aFont);
// get text width for current font size
int x = fm.width(aText);
// get text height for current font size
int y = fm.height();
// check if text fits widget
if ((x > wd_width) || (y > wd_hgt))
{
// saving maximum possible size of font
fontPointSize = i-1;
break;
}
}
aFont.setPointSize(fontPointSize);
// as a result - we get already resized aFont
}

--Avis 8:29, 30 November 2010 (UTC)

This page was last modified on 11 October 2012, at 04:17.
161 page views in the last 30 days.
×