5

Please note: This question is about Qt C++ framework, not normal Java API. In Java, this question has already been answered.

In the main menu of my application, I really don't want to worry about different kinds of screen rotation. I would like to disable screen rotation until user goes to other views where screen rotation is meaningful. For main menu, I want to use portrait view only.

How to achieve that? How to control app screen rotation?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • 1
    If I’m not mistaken (i.e., it was added to Qt in the last half year or so) you need use native API for that (as you want to do it at runtime). I wrote a small wrapper class for that in one my projects, to do this on Android and iOS. – Frank Osterfeld Dec 16 '15 at 13:31
  • possibly related: [Landscape mode in Qt-Android application](https://stackoverflow.com/questions/41546117/landscape-mode-in-qt-android-application) – pasbi Mar 13 '19 at 14:21
  • @pasbi Not really, the solution for permanent landscape mode was already known back in 2015 when I asked this question. But this question is about changing the mode to landscape temporarily, such as you would if you wanted to play a video on screen. – Tomáš Zato Mar 13 '19 at 14:29
  • @TomášZato hm... I'm trying to lock the screen to landscape permanently and I'm facing the exactly same issues as described in the linked question/answer. I'd be happy with a dynamic solution as described here, too. So I guess from this point of view they are related (but certainly not a duplicate). – pasbi Mar 13 '19 at 14:34

1 Answers1

1

You can set the orientation using

QAndroidJniObject activity = QtAndroid::androidActivity();
activity.callMethod<void>("setRequestedOrientation", "(I)V", orientation);

where orientation is an int representing the orientation. You can copy the codes provided in the documentation (e.g., 0 to lock landscape, 1 to lock portrait and -1 to unlock any rotation), but I recommend to use, e.g.,

QAndroidJniObject::getStaticField<int>("android.content.pm.ActivityInfo", "SCREEN_ORIENTATION_LANDSCAPE");

to get the code values.

This answer is based on https://stackoverflow.com/a/38947312/4248972. The difference is that this answer also explains how to unlock rotation again and provides a sample to show how to get the orientation codes without manual copying.

pasbi
  • 2,037
  • 1
  • 20
  • 32