Tomáš Zato
Tomáš Zato

Reputation: 53307

Temporarily, programmatically disable screen rotation in 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?

Upvotes: 5

Views: 2108

Answers (1)

pasbi
pasbi

Reputation: 2179

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.

Upvotes: 1

Related Questions