Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily, programmatically disable screen rotation in Qt

Tags:

c++

android

qt

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?

like image 928
Tomáš Zato - Reinstate Monica Avatar asked Nov 09 '22 23:11

Tomáš Zato - Reinstate Monica


1 Answers

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.

like image 64
pasbi Avatar answered Nov 14 '22 23:11

pasbi