QML screen orientation lock

时光怂恿深爱的人放手 提交于 2019-12-22 04:27:14

问题


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-

  1. Create an out-of-source directory - let's call it "templates" (can be anything)
  2. Copy the AndroidManifest.xml into "templates" from - <path to qt installation>/android_armv7/src/android/templates/AndroidManifest.xml
  3. 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.
  4. 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"

  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!