大家好,又见面了,我是你们的朋友全栈君。
QT += core gui sensors positioning
其中sensors是获取手机上传感器数据的组件,positioning是获取位置信息的组件
1、获取陀螺仪传感器数据
#include <QGyroscope>
QGyroscope *gyroscope;
QGyroscopeReading *reader;
gyroscope = new QGyroscope(this);
gyroscope->start();
reader = gyroscope->reading();
qreal x = reader->x();
qreal y = reader->y();
qreal z = reader->z();
2、获取加速度传感器数据
#include <QAccelerometer>
QAccelerometer *acceler;
QAccelerometerReading *accelereader;
acceler = new QAccelerometer(this);
acceler->setAccelerationMode(QAccelerometer::Combined);
acceler->start();
accelereader = acceler->reading();
qreal x = accelereader->x();
qreal y = accelereader->y();
qreal z = accelereader->z();
3、获取光线强度传感器数据
#include <QLightSensor>
QLightSensor *lightSensor;
QLightReading *lightReading;
lightSensor = new QLightSensor(this);
lightSensor->start();
lightReading = lightSensor->reading();
qreal lux = lightReading->lux();
4、获取电磁传感器数据
#include <QMagnetometer>
QMagnetometer *magnetoMeter;
QMagnetometerReading *magnetometerReading;
magnetoMeter = new QMagnetometer(this);
magnetoMeter->start();
magnetometerReading = magnetoMeter->reading();
x = magnetometerReading->x();
y = magnetometerReading->y();
z = magnetometerReading->z();
5、获取接近传感器数据
#include <QProximitySensor>
QProximitySensor *proximitySensor;
QProximityReading *proximityReading;
proximitySensor = new QProximitySensor(this);
proximitySensor->start();
proximityReading = proximitySensor->reading();
if(proximityReading->close())
{
ui->label_30->setText("接近传感器:接近");
}
else
{
ui->label_30->setText("接近传感器:远离");
}
6、旋转传感器数据读取
#include <QRotationSensor>
QRotationSensor *rotationSensor;
QRotationReading *rotationReading;
rotationSensor = new QRotationSensor(this);
rotationSensor->start();
rotationReading = rotationSensor->reading();
ui->lcdNumber_11->display(rotationReading->x());
ui->lcdNumber_12->display(rotationReading->y());
ui->lcdNumber_13->display(rotationReading->z());
7、获取手机位置相关信息
#include <QGeoPositionInfoSource>
#include <QGeoCoordinate>
#include <QGeoPositionInfo>
double nowGroundSpeed=0.0;
double nowVerticalSpeed=0.0;
double nowMagneticVariation=0.0;
double nowHorizontalAccuracy=0.0;
double nowVerticalAccuracy=0.0;
double nowLongitude=0.0;
double nowLatitude=0.0;
double nowDirection=0.0;
QString nowTimes="";
QGeoPositionInfoSource *source;
source = QGeoPositionInfoSource::createDefaultSource(this);
if (source) {
connect(source, SIGNAL(positionUpdated(const QGeoPositionInfo&)),
this, SLOT(positionUpdated(const QGeoPositionInfo&)));
source->setUpdateInterval(200);//设置刷新时间 单位毫秒
source->startUpdates();//启动设备
}
void MainWindow::positionUpdated(const QGeoPositionInfo &info)
{
nowLongitude = info.coordinate().longitude();//经度
nowLatitude = info.coordinate().latitude();//纬度
nowDirection = info.attribute(QGeoPositionInfo::Direction);
nowGroundSpeed = info.attribute(QGeoPositionInfo::GroundSpeed);
nowVerticalSpeed = info.attribute(QGeoPositionInfo::VerticalSpeed);
nowMagneticVariation = info.attribute(QGeoPositionInfo::MagneticVariation);
nowHorizontalAccuracy = info.attribute(QGeoPositionInfo::HorizontalAccuracy);
nowVerticalAccuracy = info.attribute(QGeoPositionInfo::VerticalAccuracy);
nowTimes = info.timestamp().toString();
ui->label_10->setText(nowTimes);
ui->label_12->setText(QString::number(nowLongitude,10,7));
ui->label_14->setText(QString::number(nowLatitude,10,7));
ui->label_16->setText(QString::number(nowGroundSpeed,10,7));
ui->label_18->setText(QString::number(nowVerticalSpeed,10,7));
ui->label_20->setText(QString::number(nowMagneticVariation,10,7));
ui->label_22->setText(QString::number(nowDirection,10,7));
ui->label_24->setText(QString::number(nowHorizontalAccuracy,10,7)+"
"+QString::number(nowVerticalAccuracy,10,7));
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/158834.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...