传感器展示
传感器展示示例通过可视化示例演示传感器的使用。
概览
应用程序启动时会显示一个菜单,菜单上有每个传感器的子视图按钮。传感器视图将给定的传感器实例化,以数字形式显示传感器的值,并以简单的图形表示将其可视化。
主菜单
主视图显示带有应用程序名称的标题和每个子视图的按钮,每个子视图由ColumnLayout
均匀排列。StackView
管理子视图和主菜单之间的导航。应用程序在启动时会检查传感器的可用性,并禁用不可用传感器的按钮。
注: 为简化示例,启动时只检查一次传感器的可用性。
加速计视图
加速度计视图显示当前设备的加速度值,并以与设备加速度相反的速度围绕图像移动,使图像具有与设备移动成比例的惯性效果。
围绕图像移动是通过加速度计onReadingChanged
方法实现的。
Accelerometer { id: accelerometer property real x: 0 property real y: 0 property real z: 0 active: true dataRate: 25 onReadingChanged: { x = (reading as AccelerometerReading).x y = (reading as AccelerometerReading).y z = (reading as AccelerometerReading).z imageTranslation.x = -x * 10 imageTranslation.y = y * 10 } }
每当有新的加速度计值出现时,图像的平移坐标就会相应更新。
近距离视图
当设备的近距离传感器被覆盖时,近距离视图就会显示放大的图像。
罗盘视图
指南针视图显示的指南针图像会根据指南针传感器的读数值进行旋转,使指南针转向北方。
磁力计视图
磁强计视图显示的磁铁图像的旋转角度由 x 和 y 磁强计值所给出的矢量旋转角度决定。其结果一般与罗盘给出的旋转角度相同,展示了如何使用磁力计读数的一个用例。由于磁力计可提供所有三个轴的读数,因此可以更自由地使用这些读数。
Magnetometer { id: magnetometer active: true dataRate: 25 onReadingChanged: { root.magnetometerX = (reading as MagnetometerReading).x root.magnetometerY = (reading as MagnetometerReading).y root.magnetometerZ = (reading as MagnetometerReading).z root.magnetRotation = ((Math.atan2(root.magnetometerX, root.magnetometerY) / Math.PI) * 180) } }
陀螺仪视图
陀螺仪视图也显示围绕三个轴旋转的图像,其旋转量由陀螺仪读数计算得出。由于陀螺仪可提供绕三个空间轴的相对旋转变化,而读数更新之间的时间可能不同,因此读数的时间会被存储,旋转变化会根据读数更新之间的时间进行归一化。
Gyroscope { id: gyroscope property var lastTimeStamp: 0 property real x: 0 property real y: 0 property real z: 0 active: true dataRate: 25 onReadingChanged: { x = (reading as GyroscopeReading).x y = (reading as GyroscopeReading).y z = (reading as GyroscopeReading).z let firstCall = false if (lastTimeStamp == 0) { firstCall = true } let timeSinceLast = reading.timestamp - lastTimeStamp lastTimeStamp = reading.timestamp //Skipping the initial time jump from 0 if (firstCall === true) return let normalizedX = x * (timeSinceLast / 1000000) imageXRotation.angle += normalizedX let normalizedY = y * (timeSinceLast / 1000000) imageYRotation.angle -= normalizedY let normalizedZ = z * (timeSinceLast / 1000000) imageZRotation.angle += normalizedZ } }
按下重置按钮后,图像旋转将重置为 0。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.