基于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);
}

Copyright © 2016 www.91arm.com 【91创客学堂】