×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Handle change in screen orientation in Qt

From Nokia Developer Wiki
Jump to: navigation, search
fasttrack (Talk | contribs)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
(20 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{ReviewerApproved}}
+
{{Archived|timestamp=20120213040924|user=[[User:Hamishwillee|&lt;br /&gt;----]]|[[:Category:Qt Quick|Qt Quick]] should be used for all UI development on mobile devices. The approach described in this article (based on {{Qapiname|QWidget}}) is deprecated.}}
 
+
[[Category:Qt C++ UI]][[Category:UI]][[Category:Code Examples]]
{{CodeSnippet
+
{{Abstract|This code example shows how to get notification when the screen mode changes using [http://doc.qt.nokia.com/stable/qwidget.html#resizeEvent QWidget::resizeEvent()]. This method gets called when the screen mode changes, so you have to implement that virtual method in your class in order to handle orientation change events.}}
|id=
+
{{ArticleMetaData <!-- v1.2 -->
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition
+
|sourcecode= [[Media:QtResizeEvent.zip]]
|devices=Nokia 5800 XpressMusic
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|category=Qt for Symbian
+
|devices= Nokia 5800 XpressMusic
|subcategory=UI
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|creationdate=June 26, 2009
+
|platform= S60 3rd Edition, FP1, FP2<br>S60 5th Edition
|keywords=QWidget::resizeEvent()
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= Self-Signed
 +
|capabilities= None
 +
|keywords= QWidget::resizeEvent()
 +
|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= 20090626
 +
|author= [[User:Savaj]]
 
}}
 
}}
  
==Overview==
+
{{Warning|'''This article does not work properly in Qt 4.7 in Symbian'''. It can be rectified with the change described below.<br />
  
This code snippets shows how to get notification screen mode from portrait to landscape and vise-versa. Sometime we need to resize our custom control with change in screen mode. Basically QWidget::resizeEvent() get called when user change screen mode, so you have to implement that virtual method in your class.
+
The issue is in the example code's {{Icode|ResizeEvent::GetScreenCoordinates()}} method. The {{Icode|GetScreenCoordinates}} uses {{Icode|QDesktopWidget::availableGeometry}} and {{Icode|QDesktopWidget::screenGeometry}} methods. In all cases these methods do not reflect ''current'' screen geometry values during {{Icode|QWidget::resizeEvent}} call as the respective values are only changed ''after'' the resizeEvent has occured. This means that e.g. during the startup of the application these may return wrong values as the values are not yet set properly.
  
This snippet can be self-signed. As it does not use any API which require developer/symbian signed certificate.
+
If the whole screen geometry information is needed from QDesktopWidget it's advised to connect to QApplication::desktop()'s workAreaResized(int) signal and call {{Icode|QApplication::desktop()->availableGeometry()}} in the connected slot function. This works in Qt 4.7.3 in Symbian but not (due to bug) in Qt 4.6.
 
+
==Preconditions==
+
 
+
* [http://pepper.troll.no/s60prereleases/ Download the latest Qt for S60 distribution from Qt].
+
* Install Qt for S60:[[Installing Qt on S60]]
+
* Check this link for installation guide: [http://pepper.troll.no/s60prereleases/doc/install-s60.html How to install the package].
+
* Go through this article: [[Getting started with Qt for S60]]
+
  
 +
See following bug report for more details. The [http://bugreports.qt.nokia.com/browse/QTBUG-14058 QTBUG-14058]'s fix introduces documentation for QDesktopWidget on this issue.
  
 +
In many cases application should however get simply the geometry of the main window using {{Icode|QMainWindow::width()}} and {{Icode|::height()}} methods during {{Icode|QMainWindow}}'s {{Icode|resizeEvent}}.}}
  
 
==Source==
 
==Source==
  
<code cpp>
+
<code cpp-qt>
 
//resizeEvent() method get called when user change screen mode.
 
//resizeEvent() method get called when user change screen mode.
 
void ResizeEvent::resizeEvent (QResizeEvent* event)
 
void ResizeEvent::resizeEvent (QResizeEvent* event)
Line 44: Line 53:
  
 
'''Normal screen'''<br><br>
 
'''Normal screen'''<br><br>
[[Image:ResizeEvent2.JPG]]
+
[[File:ResizeEvent2.JPG]]
  
 
'''Screen after changing mode to portrait'''<br><br>
 
'''Screen after changing mode to portrait'''<br><br>
[[Image:ResizeEvent1.JPG]]
+
[[File:ResizeEvent1.JPG]]
  
 
==Related link==
 
==Related link==
* [[CS001437 - Listening for screen orientation changes in Qt]]
+
* [[Archived:Listening for screen orientation changes in Qt]]
  
 
==Download Code Example==
 
==Download Code Example==
  
* The [[Media:QtResizeEvent.zip|Code Example]] show new screen size when user change screen mode. This example is tested on Nokia 5800 XpressMusic.
+
* [[File:QtResizeEvent.zip]] shows new screen size when user change screen mode. This example is tested on Nokia 5800 XpressMusic.
<br>
+
<br>[[Category:MeeGo Harmattan]] [[Category:Symbian]]
 
+
 
+
 
+
[[Category:Qt]][[Category:Qt for Symbian]][[Category:Code Examples]][[Category:UI]]
+

Revision as of 04:14, 11 October 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.

This code example shows how to get notification when the screen mode changes using QWidget::resizeEvent(). This method gets called when the screen mode changes, so you have to implement that virtual method in your class in order to handle orientation change events.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QWidget::resizeEvent()
Created: savaj (26 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)

Warning.pngWarning: This article does not work properly in Qt 4.7 in Symbian. It can be rectified with the change described below.

The issue is in the example code's ResizeEvent::GetScreenCoordinates() method. The GetScreenCoordinates uses QDesktopWidget::availableGeometry and QDesktopWidget::screenGeometry methods. In all cases these methods do not reflect current screen geometry values during QWidget::resizeEvent call as the respective values are only changed after the resizeEvent has occured. This means that e.g. during the startup of the application these may return wrong values as the values are not yet set properly.

If the whole screen geometry information is needed from QDesktopWidget it's advised to connect to QApplication::desktop()'s workAreaResized(int) signal and call QApplication::desktop()->availableGeometry() in the connected slot function. This works in Qt 4.7.3 in Symbian but not (due to bug) in Qt 4.6.

See following bug report for more details. The QTBUG-14058's fix introduces documentation for QDesktopWidget on this issue.

In many cases application should however get simply the geometry of the main window using QMainWindow::width() and ::height() methods during QMainWindow's resizeEvent.

Contents

Source

//resizeEvent() method get called when user change screen mode.
void ResizeEvent::resizeEvent (QResizeEvent* event)
{
QSize widgetSize = event->size();
//Resize your custom control according to new size.
QMainWindow::resizeEvent(event);
}

Postconditions

The code snippet is expected to notify change in screen mode.

Normal screen

ResizeEvent2.JPG

Screen after changing mode to portrait

ResizeEvent1.JPG

Related link

Download Code Example

  • File:QtResizeEvent.zip shows new screen size when user change screen mode. This example is tested on Nokia 5800 XpressMusic.


123 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×