Archived:Listening for screen orientation changes in Qt

From Nokia Developer Wiki
Jump to: navigation, search

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 (using C++ for the Qt app UI) is deprecated.

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Keywords: QWidget::resizeEvent(), QMainWindow::resizeEvent()
Created: tepaa (08 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)



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.



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

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

See also

Archived:Implementing a layout manager in Qt

Archived:Dynamic Layout handling with QWidget


Layout orientation and size change are handled. You will get a notification when the user changes the screen mode.

This page was last modified on 11 October 2012, at 04:14.
103 page views in the last 30 days.