Archived:Listening for screen orientation changes in Qt
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.
Article Metadata
Tested with
Compatibility
S60 5th Edition
Article
Contents |
Overview
This code snippet demonstrates how to listen for screen or layout orientation changes. When using Qt layout managers, they handle the layout changes and you generally do not need to worry about them.
On custom UI applications that do not use layout managers, you have to handle screen orientation and size changes by implementing a virtual QWidget::resizeEvent().
The event handler resizeEvent() of the base class QWidget is called whenever the user changes the screen mode. This event handler can be reimplemented in a subclass to receive widget resize events which are passed in the event parameter. QMainWindow is a subclass of QWidget and it contains resizeEvent() as well, so you can also get the resize event from QMainWindow if you implement it in your application.
This snippet can be self-signed.
Preconditions
- Install Qt SDK
Source
Solution 1: Requesting the new size from QResizeEvent
void QMyWidget::resizeEvent (QResizeEvent* event)
{
QSize widgetSize = event->size();
// TODO: You have new size of the screen
// do your new layout
// Call base class impl
QWidget::resizeEvent(event);
}
Solution 2: Requesting the new size from QMainWindow's central widget
void QMyMainWindow::resizeEvent (QResizeEvent* event)
{
QSize widgetSize = centralWidget()->size();
// TODO: You have new size of the screen
// do your new layout
// Call base class impl
QMainWindow::resizeEvent(event);
}
See also
Archived:Implementing a layout manager in Qt
Archived:Dynamic Layout handling with QWidget
Postconditions
Layout orientation and size change are handled. You will get a notification when the user changes the screen mode.


Can i know the reason of removing screenshots and code examples from this article?
-> The reason was that these were meant to be reviewed by Nokia as is. After the review Forum Nokia will lock this article.
[Seppo, Forum Nokia]: Yes, that's the case. Knowledge base articles (including Forum Nokia code snippets) differ from other Forum Nokia Wiki content in being official content, verified by Forum Nokia. Even with the risk of losing very valuable community contribution in these articles, we need to keep them protected in order to ensure our publication process and guarantee the validity and compatibility promise of the snippets.
In the code snippets creation process, the articles have been open for a short period between creation and "official publication". This has obviously given an impression that they would be ordinary articles that anyone can edit. To avoid confusion and streamline the publication process, we consider creating the future snippets in the hidden domain so that they appear directly as protected Forum Nokia articles.
We encourage developers to utilize comments tab of knowledge base articles to provide feedback. We can then evaluate the feedback and implement change requests.
All other Wiki articles are still open for our developer community!
[ALKAN-]: Hi!
Thanks for this sample code, as I was able to get started with Qt on Symbian! I have never programmed neither for Qt nor for Symbian before, so I used your example as my "Hello world":) I also found that some moments in this code are somewhat misleading:
1. There is a slot ResizeEvent::HandleResourceChange - this slot is not described and never called. Its name may suggest that this is a handler for some "resource change" event, but this is not the case.
2. ResizeEvent::resizeEvent contains string "QSize widgetSize = event->size();", but nobody later uses widgetSize. It looks like that string came from QWidget::resizeEvent documentation and nobody bothered to remove this unused string
3. Nobody explained that all this code works only because user class called from main.cpp by w.showMximized() method. If anybody try to create its own code sample he finds that resizeEvent is called only once, during the startup, for almost no reason. Reason for that is that Qt wizard creates application which call main window with .show() method, so application window has its custom size which is not related to system window size, and nobody inform such application that screen layout is changed. In this example main window is called by .showMaximized() method, this means that system changes window size each time when screen layout changes, so resizeEvent is called each window resize
But anyway, thanks for this sample as using it I started understand some moments on Qt for Symbian