简体   繁体   English

QML屏幕方向锁定

[英]QML screen orientation lock

I need to lock the screen to a specific orientation for an android app that I'm making. 我需要将屏幕锁定到我正在制作的Android应用程序的特定方向。 is there a way to do that in QML? 有没有办法在QML中做到这一点? Google didn't get me any answers. 谷歌没有给我任何答案。
I am using Qt 5.2. 我使用的是Qt 5.2。

You can basically use the Screen.orientation property for reading only. 您基本上可以使用Screen.orientation属性进行读取。 See the following example for details: 有关详细信息,请参阅以下示例

http://qt-project.org/doc/qt-5/qtquick-window-screeninfo-qml.html 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: 截至今天的正确方法是使用Android清单文件,这是由Digia的Qt Android开发人员为他的QtHangMan游戏完成的,该游戏是:

<activity android:screenOrientation="portrait" ... 

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 将AndroidManifest.xml复制到“ 模板 ” - <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. 使用您喜欢的编辑器编辑复制的AndroidManifext.xml,并将方向从“ 未指定 ”更改为“ sensorLandscape ”(或您喜欢的任何其他内容)并保存文件。
  4. In the qmake file (*.pro) of your project, add the following line - 在项目的qmake文件(* .pro)中,添加以下行 -

    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" ANDROID_PACKAGE_SOURCE_DIR = <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 您现在应该看到Orientation固定为您在自定义AndroidManifest.xml文件中指定的任何内容

To understand what really is happening - here's the qt reference , look for ANDROID_PACKAGE_SOURCE_DIR 要了解到底发生了什么 - 这是qt参考 ,请查找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." “ANDROID_PACKAGE_SOURCE_DIR:此变量可用于指定可对默认Android软件包模板进行添加和修改的目录.androiddeployqt工具将应用程序模板从Qt复制到构建目录中,然后它将复制内容ANDROID_PACKAGE_SOURCE_DIR在此之上,覆盖任何现有文件。然后在生成的合并包上运行更新步骤,其中部分源文件被自动修改以反映您的其他设置。例如,如果您想要创建自定义AndroidManifest。您的应用程序的xml,然后将其直接放入此变量中指定的文件夹中。您还可以在ANDROID_PACKAGE_SOURCE_DIR / src中添加自定义Java文件。“

QtCreator - 4.2.1, Qt - 5.8 QtCreator - 4.2.1,Qt - 5.8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM