×
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
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
 
[[Category:Qt]][[Category:UI]]
 
[[Category:Qt]][[Category:UI]]
{{ArticleMetaData
+
{{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=UI
+
|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 -->
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
+
|translated-by= <!-- [[User:XXXX]] -->
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
+
|translated-from-title= <!-- Title only -->
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
+
|translated-from-id= <!-- Id of translated revision -->
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
+
|review-by= <!-- After re-review: [[User:username]] -->
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
+
|update-by= <!-- After significant update: [[User:username]]-->
|author=[[User:Avis]]
+
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 30 November 2010
 +
|author= [[User:Avis]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= UI
 
}}
 
}}
  
Line 21: Line 28:
 
== Basic Idea ==
 
== Basic Idea ==
  
{{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 [[CS001349 - Calculating text width in Qt]] }}
+
{{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.
 
And here is contrary task: set font size to maximally fit into widget's size. Simple loop will help.
  
<code cpp>
+
<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 59: 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.
113 page views in the last 30 days.
×