基于Qt的车载GPS监控系统(7)数据记录

基于Qt的车载GPS监控系统(7)数据记录

数据日志显示界面

这里写图片描述

系统数据记录功能实现说明
1.通过一个线程定时记录系统当前的温度数据。
2.通过数据日志界面显示保存的日志数据

数据记录线程代码

#ifndef THREAD_H
#define THREAD_H
#include"dht9000.h"
#include<QThread>

class Thread:public QThread
{
    Q_OBJECT
public:
    Thread();
public slots:
    void goAddTime();
protected:
    void run();
    int is_trigger(float temp);
    struct st_sys_dev g_dev[1];
    int time_log;
private:
    float ftemp;
};

#endif // THREAD_H
#include<thread.h>
#include<dht9000.h>
#include<QTimer>
#include<QTime>
#include<QDate>
#include<QDebug>

Thread::Thread()
{
    time_log = 0;
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(goAddTime()));
    timer->start(1000);
    qDebug() << "thread start...";
}

void Thread::goAddTime()
{
    time_log++;//控制记录间隔时间
    ftemp = rand()%100;//产生随机的温度
}

int Thread::is_trigger(float temp)
{
        char flag = 0;
        int tem_max,tem_min;
        tem_max = g_dev->alarm_set.temp_upper;
        tem_min = g_dev->alarm_set.temp_lower;
        //判断一下极限值是否报警
        if(temp>tem_max || temp<tem_min)
            flag = 1;
        return flag;
}

void Thread::run()
{
    while(1){
    //  if(time_log>=(g_dev->log_time*60) && time_log>=60){
        if(time_log>=30){
                load_dev(g_dev);
                time_log = 0;
                if(g_dev->log_index >= MAX_RECORD)  g_dev->log_index = 0;
                if(g_dev->log_index < 0) g_dev->log_index = 0;
                g_dev->logd[g_dev->log_index].log_temp = ftemp;
                g_dev->logd[g_dev->log_index].state = is_trigger(ftemp);
                QDate date = QDate::currentDate();
                QString text = date.toString("yy.MM.dd");
                strcpy(g_dev->logd[g_dev->log_index].date,text.toAscii().data());
                QTime time = QTime::currentTime();
                QString text2 = time.toString("hh:mm:ss");
                strcpy(g_dev->logd[g_dev->log_index].time,text2.toAscii().data());
                if(g_dev->log_count++ > MAX_RECORD)  g_dev->log_count=MAX_RECORD;
                qDebug() << "save" << g_dev->logd[g_dev->log_index].log_humi;
                qDebug() << "log_index" << g_dev->log_index;
                g_dev->log_index++;
                save_dev(g_dev);
        }
    }
}

数据日志显示代码

#ifndef RECORDDIALOG_H
#define RECORDDIALOG_H
#include "setdialog.h"
#include<QTableWidget>
#include"dht9000.h"

class RecordDialog:public SetDialog
{
    Q_OBJECT
public:
    RecordDialog(QWidget *parent = 0);
public slots:
private:
    void creatColTitle(QTableWidget *table,int col,char* title,char*image);
    QTableWidget *table;

};
#endif // RECORDDIALOG_H
#include<recorddialog.h>

RecordDialog::RecordDialog(QWidget *parent)
    :SetDialog(parent)
{
    this->lineTitle->setText(tr("数据日志"));
    load_dev(g_dev);
    int row;
    int col;
    table = new QTableWidget(this);
    table->setRowCount(20);
    table->setColumnCount(5);
    char  title[20];
    QString itemStr;
    QTableWidgetItem *newItem;
    for(col = 0;col < 5; col++){
        switch(col){
            case 0:
                strcpy(title,"状态");
                break;
            case 1:
                strcpy(title,"温度");
                break;
            case 2:
                strcpy(title,"湿度");
                break;
            case 3:strcpy(title,"日期");break;
            case 4:strcpy(title,"时间");break;
        }
        creatColTitle(table,col,title,":/images/1.png");
        for(row = 0;row < 20;row++){
            switch(col){
                case 0:
                    strcpy(title,"状态");
                    itemStr.setNum(g_dev->logd[row].state);
                    break;
                case 1:
                    strcpy(title,"温度");
                    itemStr.setNum(g_dev->logd[row].log_temp);
                    break;
                case 2:
                    strcpy(title,"湿度");
                    itemStr.setNum(g_dev->logd[row].log_humi);
                    break;
                case 3:
                    strcpy(title,"日期");
                    //itemStr=QString(g_dev->logd[row].date);
                    break;
                case 4:
                    strcpy(title,"时间");
                    //itemStr=QString(g_dev->logd[row].time);
                    break;
            }
            newItem = new QTableWidgetItem(itemStr);
            table->setItem(row, col, newItem);
        }
    }
    table->setGeometry(QRect(140, 130, 540, 230));
}

void RecordDialog::creatColTitle(QTableWidget *table,int col,char* title,char*image){
    QTableWidgetItem *headerItem = new QTableWidgetItem(tr(title));
    headerItem->setIcon(QIcon(QPixmap(image)));
    headerItem->setTextAlignment(Qt::AlignHCenter);
    table->setHorizontalHeaderItem(col,headerItem);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值