#include /n#include /n#include /n#include /n#include /n/nUsersWidget::UsersWidget(int userid,QWidget *parent) : /n QWidget(parent), /n ui(new Ui::UsersWidget), /n userid(userid) /n{/n ui->setupUi(this); /n displayUsersWidget(); /n connect(ui->lights,&QToolButton::clicked,this,[=]{ /n Lights *w = new Lights(userid); /n w->show();}); /n connect(ui->air_condition,&QToolButton::clicked,this,[=]{ /n AirCondition *w = new AirCondition(userid); /n w->show();}); /n connect(ui->curtains,&QToolButton::clicked,this,[=]{ /n Curtains *w = new Curtains(userid); /n w->show();}); /n connect(ui->humidifiers,&QToolButton::clicked,this,[=]{ /n Humidiffer *w = new Humidiffer(userid); /n w->show();}); /n connect(ui->tables,&QToolButton::clicked,this,[=]{ /n Information *w = new Information; /n w->show();}); /n connect(ui->wifi,&QToolButton::clicked,this,[=]{ /n Procession *w = new Procession(userid); /n w->show();}); /n //模式信号和槽连接 /n connect(ui->mode, QOverload::of(&QComboBox::currentIndexChanged), /n this, &UsersWidget::on_mode_currentIndexChanged); /n loadLastStatus(); /n/n/n/n} /n/nUsersWidget::~UsersWidget() /n{/n delete ui; /n} /n/nvoid UsersWidget::displayUsersWidget() /n{/n setWindowTitle('智能家居总览图'); /n setFixedSize(900,600); /n/n} /n/nvoid UsersWidget::loadLastStatus() /n{/n qDebug()<userid; /n QString state = getLastState(this->userid); /n qDebug()<<state; /n if (state.compare('日常模式') == 0) { /n ui->mode->setCurrentIndex(1); /n } else if (state.compare('睡眠模式') == 0) { /n ui->mode->setCurrentIndex(2); /n } else if (state.compare('节能模式') == 0) { /n ui->mode->setCurrentIndex(3); /n } else { /n ui->mode->setCurrentIndex(0); /n } /n/n} /n/nvoid UsersWidget::saveLastStatus() /n{/n qDebug()<userid; /n QString Mode = ui->mode->currentData().toString(); /n updateLastMode(this->userid, Mode); /n/n} /n/nvoid UsersWidget::on_mode_currentIndexChanged(int mode) /n{/n switch (mode) /n { /n case 1: /n { /n updateSmartHomeStatus(userid, '智能灯', '关闭', '0'); /n updateSmartHomeStatus(userid, '空调', '开启', '25'); /n updateSmartHomeStatus(userid, '加湿器', '开启', '60'); /n updateSmartHomeStatus(userid, '窗帘', '开启', '50'); /n/n break; /n } /n case 2: /n { /n updateSmartHomeStatus(userid, '智能灯', '开启', '20'); /n updateSmartHomeStatus(userid, '空调', '开启', '27'); /n updateSmartHomeStatus(userid, '加湿器', '开启', '50'); /n updateSmartHomeStatus(userid, '窗帘', '开启', '100'); /n break; /n } /n case 3: /n { /n updateSmartHomeStatus(userid, '智能灯', '开启', '50'); /n updateSmartHomeStatus(userid, '空调', '开启', '26'); /n updateSmartHomeStatus(userid, '加湿器', '开启', '40'); /n updateSmartHomeStatus(userid, '窗帘', '开启', '30'); /n break; /n } /n default:; /n/n } /n saveLastStatus(); /n} /n/nQString UsersWidget::getLastState(int userid) /n{/n Sqlite::sqlite_init(); /n QString queryStr = QString(/'SELECT mode FROM Status WHERE uid = %1 ORDER BY sid DESC LIMIT 1/').arg(userid); /n qDebug()<<queryStr<<userid; /n QSqlQuery query; /n qDebug ()<<query.exec(queryStr); /n if (query.exec(queryStr) && query.next()) /n { /n return query.value(0).toString(); /n } /n else /n { /n qDebug() << /'得到上一次状态错误: /' << query.lastError().text(); /n return /'/'; /n } /n} /n/nvoid UsersWidget::updateLastMode(int userid,QString mode) /n{/n Sqlite::sqlite_init(); /n QString queryStr = QString(/'UPDATE Status SET mode = //'%1//' WHERE uid = %2 /').arg(mode).arg(userid); /n QSqlQuery query; /n if (!query.exec(queryStr)) /n { /n qDebug() << /'更新模式失败: /' << query.lastError().text(); /n } /n} /nvoid UsersWidget::closeEvent(QCloseEvent *e) /n{/n if(e->spontaneous()) /n { /n //窗口关闭时弹出的提示窗口 /n QMessageBox::StandardButton reply; /n/n reply = QMessageBox::information(this,/'提示/',/'确认退出智能管家系统?/',QMessageBox::Yes|QMessageBox::No); /n if(reply == QMessageBox::Yes){ /n //若用户点击确认,则接收这个事件,当前窗口会关闭 /n e->accept(); /n }else{ /n //若用户点击取消,则忽略这个事件,当前窗口不会关闭 /n e->ignore(); /n } /n } /n} /n/n运行结果 /n123 /n数据库打开成功! /n/'SELECT mode FROM Status WHERE uid = 123 ORDER BY sid DESC LIMIT 1/' 123 /ntrue /n/'/' /n数据库打开成功! /n数据库打开成功! /n数据库打开成功! /n数据库打开成功! /n123 /n数据库打开成功! /n更新模式失败: /'near ///'WHERE///': syntax error Unable to execute statement/' /n数据库打开成功! /n数据库打开成功! /n数据库打开成功! /n数据库打开成功! /n123 /n数据库打开成功! /n更新模式失败: /'near ///'WHERE///': syntax error Unable to execute statement/

智能家居系统用户界面 - Qt 开发

原文地址: http://www.cveoy.top/t/topic/fCZj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录