センサーの事例紹介
「センサーの活用例」では、視覚的な例を用いてセンサーの使用方法を解説しています。

概要
起動時、アプリケーションは各センサーのサブビューへのボタンが並んだメニューを表示します。センサービューは、指定されたセンサーをインスタンス化し、センサーの値を数値として表示するとともに、シンプルなグラフィカルな表現で視覚化します。
メインメニュー
メインビューには、アプリケーション名のタイトルと、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軸の磁力計値によって表されるベクトルの回転角に基づいて、磁石の画像が回転して表示されます。これにより、一般的にコンパスと同じ回転方向が示され、磁力計の測定値の活用例の一つが示されます。 磁力計は3軸すべてに沿った測定値を提供するため、これらの測定値の活用方法にはより多くの自由度があります。
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)
}
}ジャイロスコープビュー

ジャイロスコープビューも、ジャイロスコープの測定値から計算された角度で3つの軸を中心に回転した画像を表示します。 ジャイロスコープは3つの空間軸周りの相対的な回転変化を提供し、測定値の更新間隔は変動する可能性があるため、測定時刻が保存され、更新間隔に基づいて回転変化が正規化されます。
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 にリセットされます。
© 2026 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.