欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

qt 读取手机传感器

程序员文章站 2022-04-19 10:22:04
...

手机界面对qt 里面的ui界面支持很低,如果想做个好看的界面还是用qml去搞吧。通过assitant的查找,发现手机有如下传感器可以使用:

qt 读取手机传感器

这里我就挑几个来说,其他的都类似。下面是QAccelerometer 和 Gyroscope ,代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    setAttribute(Qt::AA_SynthesizeTouchForUnhandledMouseEvents);
    setAttribute(Qt::WA_AcceptTouchEvents);
    ui->setupUi(this);
    gyroscope = new QGyroscope(this);
    gyroscope->start();
    reader = gyroscope->reading();
    acceler = new QAccelerometer(this);
    acceler->setAccelerationMode(QAccelerometer::Combined);
    acceler->start();
    accelereader = acceler->reading();
    qreal x = reader->x();
    qreal y = reader->y();
    qreal z = reader->z();
    ui->lcdNumber->display(x);
    ui->lcdNumber->display(y);
    ui->lcdNumber->display(z);
    x = accelereader->x();
    y = accelereader->y();
    z = accelereader->z();
    ui->lcdNumber_4->display(x);
    ui->lcdNumber_5->display(y);
    ui->lcdNumber_6->display(z);
    timer = new QTimer();
    timer->setInterval(1000);
    connect(timer,SIGNAL(timeout()),this,SLOT(on_pushButton_clicked()));
    timer->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
        qreal x = reader->x();
        qreal y = reader->y();
        qreal z = reader->z();
        ui->lcdNumber->display(x);
        ui->lcdNumber_2->display(y);
        ui->lcdNumber_3->display(z);
        x = accelereader->x();
        y = accelereader->y();
        z = accelereader->z();
        ui->lcdNumber_4->display(x);
        ui->lcdNumber_5->display(y);
        ui->lcdNumber_6->display(z);
}

qt 读取手机传感器
相关标签: sensor qt