Archived:Handle change in screen orientation in Qt
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot update - Fix ReviewerApproval and ArticleMetaData etc) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Tidy wiki text) |
||
| Line 1: | Line 1: | ||
| + | [[Category:QWidget UI]][[Category:UI]][[Category:Code Examples]] | ||
| + | {{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.}} | ||
{{Archived|timestamp=20120213040924|user=[[User:Hamishwillee|<br />----]]|[[: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.}} | {{Archived|timestamp=20120213040924|user=[[User:Hamishwillee|<br />----]]|[[: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.}} | ||
| − | |||
| − | |||
| − | |||
{{ArticleMetaData <!-- v1.1 --> | {{ArticleMetaData <!-- v1.1 --> | ||
|sourcecode= [[Media:QtResizeEvent.zip]] | |sourcecode= [[Media:QtResizeEvent.zip]] | ||
| Line 11: | Line 10: | ||
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> | |devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> | ||
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
| − | |signing= | + | |signing= Self-Signed |
| − | |capabilities= | + | |capabilities= None |
|keywords= QWidget::resizeEvent() | |keywords= QWidget::resizeEvent() | ||
|id= <!-- Article Id (Knowledge base articles only) --> | |id= <!-- Article Id (Knowledge base articles only) --> | ||
| Line 25: | Line 24: | ||
|creationdate= 20090626 | |creationdate= 20090626 | ||
|author= [[User:Savaj]] | |author= [[User:Savaj]] | ||
| − | |||
| − | |||
| − | |||
}} | }} | ||
| Line 38: | Line 34: | ||
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. | 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 | + | 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}}.}} |
| − | {{Icode|QMainWindow::width()}} and {{Icode|::height()}} methods during {{Icode|QMainWindow}}'s {{Icode|resizeEvent}}.}} | + | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
==Source== | ==Source== | ||
| Line 81: | Line 66: | ||
* [[File:QtResizeEvent.zip]] shows 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> | ||
| − | |||
| − | |||
| − | |||
| − | |||
Revision as of 07:41, 13 February 2012
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.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Article Metadata
Code Example
Tested with
Compatibility
S60 5th Edition
Platform Security
Article
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.
Screen after changing mode to portrait
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.

