问题
I need to lock the screen to a specific orientation for an android app that I'm making.
is there a way to do that in QML? Google didn't get me any answers.
I am using Qt 5.2.
回答1:
You can basically use the Screen.orientation
property for reading only. See the following example for details:
http://qt-project.org/doc/qt-5/qtquick-window-screeninfo-qml.html
The proper way as of today is to use the Android manifest file for this as done by the Qt Android developer at Digia for his QtHangMan game which is:
<activity android:screenOrientation="portrait" ...
回答2:
The way to do this is-
- Create an out-of-source directory - let's call it "templates" (can be anything)
- Copy the AndroidManifest.xml into "templates" from -
<path to qt installation>/android_armv7/src/android/templates/AndroidManifest.xml
- Edit the copied AndroidManifext.xml with your favorite editor and change the orientation from "unspecified" to "sensorLandscape" (or anything else you prefer) and save the file.
In the qmake file (*.pro) of your project, add the following line -
ANDROID_PACKAGE_SOURCE_DIR = <path to your templates directory>
#in case you added the directory one above the source directory, path to your templates directory = "../templates"Build and Run. You should now see that the Orientation is fixed to whatever you have specified in the custom AndroidManifest.xml file
To understand what really is happening - here's the qt reference, look for ANDROID_PACKAGE_SOURCE_DIR
In case the link doesn't work in future, here's the relevant bits -
"ANDROID_PACKAGE_SOURCE_DIR: This variable can be used to specify a directory where additions and modifications can be made to the default Android package template. The androiddeployqt tool will copy the application template from Qt into the build directory, and then it will copy the contents of the ANDROID_PACKAGE_SOURCE_DIR on top of this, overwriting any existing files. The update step where parts of the source files are modified automatically to reflect your other settings is then run on the resulting merged package. If you, for instance, want to make a custom AndroidManifest.xml for your application, then place this directly into the folder specified in this variable. You can also add custom Java files in ANDROID_PACKAGE_SOURCE_DIR/src."
QtCreator - 4.2.1, Qt - 5.8
来源:https://stackoverflow.com/questions/20774498/qml-screen-orientation-lock