You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android QML中QtSensors5.0的ProximitySensor使用异常求助

Troubleshooting ProximitySensor in QtSensors 5.0 QML

Hey there! Let's break down the issues you're facing with the ProximitySensor in QtSensors 5.0 for QML—since you confirmed the hardware works, the problem is almost certainly in how you're interacting with the QML API.

First, Let's Fix the Core Setup

It sounds like you might be missing some key parts of the QML sensor structure. Here's a working minimal example that should address most of your issues:

import QtQuick 2.0
import QtSensors 5.0

Item {
    width: 300
    height: 300

    // Define the sensor with an ID so you can reference it
    ProximitySensor {
        id: proximitySensor
        // Set active to true to start the sensor automatically
        active: true

        // Listen for new readings (this is where valid data will be available)
        onReadingChanged: {
            console.log("Object near?", reading.near);
            // You can also access other reading properties like timestamp here
        }
    }

    // Display the current status
    Text {
        anchors.centerIn: parent
        text: proximitySensor.reading 
            ? (proximitySensor.reading.near ? "Object is close!" : "No object nearby")
            : "Sensor starting up..."
    }

    // Example button to control the sensor (uses stop()/start(), not close())
    Button {
        text: proximitySensor.active ? "Turn Off Sensor" : "Turn On Sensor"
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        onClicked: {
            if (proximitySensor.active) {
                proximitySensor.stop();
            } else {
                proximitySensor.start();
            }
        }
    }
}

Let's Address Your Specific Issues

  1. Reading only shows deleteLater, objectName, timestamp
    This happens when you try to access the reading property before the sensor has initialized or started producing data. Always access reading either in the onReadingChanged signal handler (triggered when new data arrives) or check if reading exists first (like in the Text element above).

  2. ProximityReading.near returns undefined
    Again, this is tied to timing—you're trying to read near before the sensor has valid data. Wait for the onReadingChanged signal, or ensure the sensor is active (active: true or start() called) before accessing the property.

  3. proximitySensor.close() says "undefined"
    Two issues here:

    • You probably didn't set an id: proximitySensor on your ProximitySensor component, so the code can't find the object.
    • QtSensors uses stop() to disable the sensor, not close()—close() isn't a valid method for this class. Use proximitySensor.stop() instead.

Quick Checklist to Double-Check

  • Make sure your .pro project file includes QT += sensors to link the sensors module.
  • Confirm you have the correct import: import QtSensors 5.0 (not a newer version, since you're using 5.0).
  • Test on your target device (emulators often don't support proximity sensors, but you said your phone works, so this is covered).

内容的提问来源于stack exchange,提问作者Fagner Fonseca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:50:08