加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sleep.cpp 8.80 KB
一键复制 编辑 原始数据 按行查看 历史
#include "sleep.h"
#include "ui_sleep.h"
#include "myapp.h"
#include <QDebug>
sleep::sleep(QWidget *parent) :
QWidget(parent),
ui(new Ui::sleep)
{
ui->setupUi(this);
//sleep界面舒适度的标志位
this->Comfort_flag = 0;
sound_times_flag = 0;
sound_times = 0;
//read_in_sql();
//建立背景;
//QLabel *background = new QLabel(this);
//background->setPixmap(QPixmap(":/Rescoure_File/Main_BK.png")); // 或者使用绝对路径
//background->setGeometry(0, 0, this->width(), this->height());
//background->lower(); // 将背景放在最底层
//background->show();
QPalette palette;//设置页面背景
palette.setBrush(QPalette::Background, QBrush(QPixmap(":/Rescoure_File/1.jpg").scaled(this->size())));
this->setPalette(palette);
this->ui->pushButton->setIcon(QIcon(":/Rescoure_File/25.png"));//设置返回按钮
this->ui->pushButton->setStyleSheet("border-image:url(:/Rescoure_File/25.png); ");
//温湿度、光照获取的类对象
this->Tem_Hum = new tem_hum;
//环境数据更新时间间隔
this->timer = new QTimer;
//睡眠时长定时器
this->timer_sleep = new QTimer;
//翻身检测时间间隔
this->sleep_turn = new QTimer;
time_sleep.setHMS(00,00,00);
this->ui->label_4->setText(time_sleep.toString("hh:mm:ss"));
timer_sleep->setInterval(1000);
timer->setInterval(1000);
sleep_turn->setInterval(100);
timer->start();
//this->myVol = new VolumeGet();
connect(this->timer,&QTimer::timeout,this,&sleep::timer_slot);
connect(this->timer,&QTimer::timeout,this,&sleep::set_label_Comfort);
connect(this->timer_sleep,&QTimer::timeout,this,&sleep::timer_sleep_slot);
connect(this->sleep_turn,&QTimer::timeout,this,&sleep::sleep_turn_slot);
}
sleep::~sleep()
{
delete ui;
}
void sleep::on_pushButton_clicked()
{
this->close();
myapp::getapp()->widget->show();
}
//sleep界面环境数据展示
void sleep::set_label_data()
{
qDebug()<<sound_db;
this->Comfort_flag=0;
//this->ui->label_11->setText(QString::number(myVol->getVolumeDb()));
this->ui->label_11->setText(QString::number(sound_db,'f',1)+"dB");
this->ui->label_7->setText(QString::number(light,'f',1)+"Lux");
this->ui->label_9->setText(QString::number(tem,'f',1)+"℃");
this->ui->label_10->setText(QString::number(hum,'f',1)+"%");
//设置绘制文本对象
QPalette palette ;
palette.setColor(QPalette::WindowText, Qt::red);//设置更改字体颜色
if(light>100)
{
this->Comfort_flag++;
this->ui->label_7->setPalette(palette);
}
else
{
this->ui->label_7->setAutoFillBackground(true); // 这是必须的,因为QLabel默认是有调色板的
this->ui->label_7->setPalette(QPalette());
this->ui->label_7->setStyleSheet("background-color: transparent;");
}
if(tem>29)
{
this->Comfort_flag++;
this->ui->label_9->setPalette(palette);
}
else
{
this->ui->label_9->setAutoFillBackground(true); // 这是必须的,因为QLabel默认是有调色板的
this->ui->label_9->setPalette(QPalette());
this->ui->label_9->setStyleSheet("background-color: transparent;");
}
if(hum>69)
{
this->Comfort_flag++;
this->ui->label_10->setPalette(palette);
}
else
{
this->ui->label_10->setAutoFillBackground(true); // 这是必须的,因为QLabel默认是有调色板的
this->ui->label_10->setPalette(QPalette());
this->ui->label_10->setStyleSheet("background-color: transparent;");
}
if(sound_db > -4)
{
qDebug()<<sound_db;
this->Comfort_flag++;
this->ui->label_11->setPalette(palette);
}
else
{
this->ui->label_11->setAutoFillBackground(true); // 这是必须的,因为QLabel默认是有调色板的
this->ui->label_11->setPalette(QPalette());
this->ui->label_11->setStyleSheet("background-color: transparent;");
}
}
//舒适度判断及展示
void sleep::set_label_Comfort()
{
QPalette palette_yellow ;
palette_yellow.setColor(QPalette::WindowText, Qt::yellow);
QPalette palette_green ;
palette_green.setColor(QPalette::WindowText, Qt::green);
QPalette palette_red ;
palette_red.setColor(QPalette::WindowText, Qt::red);
switch(this->Comfort_flag)
{
case 0 :
this->ui->label_15->setText("舒适");
this->ui->label_15->setPalette(palette_green);
break;
case 1 :
this->ui->label_15->setText("较舒适");
this->ui->label_15->setPalette(palette_yellow);
break;
case 2 :
this->ui->label_15->setText("较舒适");
this->ui->label_15->setPalette(palette_yellow);
break;
case 3 :
this->ui->label_15->setText("不舒适");
this->ui->label_15->setPalette(palette_red);
break;
case 4 :
this->ui->label_15->setText("不宜入睡");
this->ui->label_15->setPalette(palette_red);
break;
}
}
void sleep::timer_slot()
{
//float*data=Tem_Hum->get_tem_hum();
Tem_Hum->get_tem();
Tem_Hum->get_light();
set_label_data();
}
//翻身次数检测
void sleep::sleep_turn_slot()
{
Tem_Hum->get_proximity();
this->ui->label_16->setText(QString::number(turn_num)+"次");
this->ui->label_5->setText(QString::number(sound_times)+"次");
if(sound_times_flag == 0)
{
if(sound_db > -8)
{
sound_times++;
sound_times_flag = 1;
}
}
if(sound_db < -11)
{
sound_times_flag = 0;
}
}
//入睡/起床按钮槽函数
void sleep::on_pushButton_2_clicked()
{
if(ui->pushButton_2->text()=="入睡")
{
//获取当前时间作为入睡时间
QDateTime fall_currentTime = QDateTime::currentDateTime();
this->fall_asleep_time = fall_currentTime.toString("yyyy-MM-dd hh:mm:ss");
qDebug()<<"入睡"<<fall_asleep_time.split("-").at(1)+"-"+fall_asleep_time.split("-").at(2).split(":").at(0)+":"+fall_asleep_time.split("-").at(2).split(":").at(1);;
//启动翻身检测
sleep_turn->start();
ui->pushButton_2->setText("起床");
//启动睡眠时长计时
timer_sleep->start();
}
else if (ui->pushButton_2->text()=="起床")
{
//获取当前时间作为起床时间
QDateTime wake_up_currentTime = QDateTime::currentDateTime();
this->wake_up_time = wake_up_currentTime.toString("yyyy-MM-dd hh:mm:ss");
qDebug()<<"起床"<<wake_up_time;
//停止翻身检测
sleep_turn->stop();
this->ui->label_16->setText("0次");
ui->pushButton_2->setText("入睡");
//停止睡眠时长计时
timer_sleep->stop();
//写入数据库
write_in_sql();
//发送信号;
// emit refreshEmit();
//弹窗提醒用户本次睡眠时长
QStringList sleep_time;
sleep_time = ui->label_4->text().split(":");
QMessageBox msg;
msg.setText("本次睡眠共:\n"+sleep_time.at(0)+"小时"+sleep_time.at(1)+"分钟\n");
msg.exec();
//QMessageBox::information(this,"提示","本次睡眠时长:\n"+sleep_time.at(0)+"小时\n"+sleep_time.at(1)+"分钟\n");
//重置睡眠时长
time_sleep.setHMS(0,0,0);
qDebug()<<time_sleep ;
this->ui->label_4->setText(time_sleep.toString("hh:mm:ss"));
//重置翻身次数和打鼾次数
sleep_turn = 0;
sound_times = 0;
}
}
void sleep::timer_sleep_slot()
{
time_sleep=time_sleep.addSecs(1);
qDebug()<<time_sleep ;
this->ui->label_4->setText(time_sleep.toString("hh:mm:ss"));
}
//写入数据库
void sleep::write_in_sql()
{
fall_asleep_time = fall_asleep_time.split("-").at(1)+"-"+fall_asleep_time.split("-").at(2).split(":").at(0)+":"+fall_asleep_time.split("-").at(2).split(":").at(1);
wake_up_time = wake_up_time.split("-").at(1)+"-"+wake_up_time.split("-").at(2).split(":").at(0)+":"+wake_up_time.split("-").at(2).split(":").at(1);
MyDate *data = MyDate::getdata();
QString sql("insert into user_sleep_info values('"+fall_asleep_time+"','"+wake_up_time+"','"+QString::number(sound_times)+"','"+QString::number(turn_num)+"','"+time_sleep.toString("hh:mm")+"');");
data->noData_exec(sql.toStdString().c_str());
}
//test 读数据库
void sleep::read_in_sql()
{
char **resultp;
int nrow;
int ncolumn;
MyDate *data = MyDate::getdata();
QString sql("select * from user_sleep_info;");
data->getData_exec(sql.toStdString().c_str(),resultp,nrow,ncolumn);
if(nrow)
{
for(int i=0; i<=nrow; i++)
{
for(int j=0; j<ncolumn;j++)
{
qDebug()<<"111"<<resultp[i*ncolumn+j]<<"\n";
}
qDebug()<<i<<i<<i;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化