Qt C++ 智能家居系统:用户管理、状态控制和数据库交互
Qt C++ 智能家居系统:用户管理、状态控制和数据库交互
本项目使用 Qt C++ 开发了一个智能家居系统,包含用户管理、智能家居状态控制功能,并使用 SQLite 数据库进行数据存储。
系统架构:
- 数据库: SQLite 数据库,包含两个表:
- 用户表:存储用户的用户名和密码。
- 智能家居状态表:记录着智能灯开关、智能灯亮度、空调开关、空调温度、加湿器开关、加湿器湿度、窗帘开关、窗帘高度、模式信息(睡眠模式、日常模式、节能模式)。
- 客户端: Qt C++ 开发的应用程序。
- 数据连接: 用户表和智能家居状态表通过键值连接建立用户和状态之间的对应关系,一个用户对应一个智能家居状态表。
主要功能:
- 用户管理:
- 用户登录:在用户管理界面输入用户名和密码进行登录。
- 用户注册:提供用户注册功能,允许新用户注册账号。
- 智能家居状态控制:
- 界面展示: 提供不同的界面控制智能灯、空调、加湿器、窗帘和模式。
- 状态更新: 使用
void updateSmartHomeStatus(const int userid, const QString& deviceName, const QString& newValue)函数更新数据库表中的状态信息。
代码示例:
#include "manage.h"
#include "users.h"
#include "ui_manage.h"
#include "userswidget.h"
#include "sqlite.h"
#include "reg.h"
#include <QPushButton>
#include <QLineEdit>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include <QMessageBox>
#include "QtDebug"
#include <QCloseEvent>
Manage::Manage(QWidget *parent) :
QWidget(parent),
ui(new Ui::Manage)
{
ui->setupUi(this);
connect(ui->loginon,&QPushButton::clicked,this,[=](){
loginUser(ui->username->text(),ui->password->text());});
connect(ui->registe,&QPushButton::clicked,this,[=](){registerUser();});
}
Manage::~Manage()
{
delete ui;
}
void Manage::loginUser(QString username,QString passwd)
{
Sqlite::sqlite_init();
QSqlQuery query;
QString sql=QString("SELECT * FROM users WHERE username = \'%1\' AND passwd = \'%2\'").arg(username).arg(passwd);
if (!query.exec(sql)) {
qDebug() << "查询失败:" << query.lastError().text();
}
//判断执行结果
if(!query.next())
{
qDebug() << "登录失败!" << query.lastError();
QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");
}
else
{
qDebug() << "登录成功!";
QMessageBox::information(this,"登录认证","登录成功");
// 获取用户ID
int userId = query.value("uid").toInt();
StatusUid(userId);
//登录成功后可以跳转到其他页面
UsersWidget *w = new UsersWidget(userId); // 传递 userId 给 UsersWidget
w->show();
this->window()->close();//关闭包含组件的窗口
}
}
void Manage::registerUser()
{
this->window()->close();
Reg *s=new Reg;
s->show();
}
#include "userswidget.h"
#include "ui_userswidget.h"
#include "lights.h"
#include "aircondition.h"
#include "curtains.h"
#include "humidiffer.h"
#include "information.h"
#include "procession.h"
#include "QCloseEvent"
#include "QMessageBox"
UsersWidget::UsersWidget(QWidget *parent, int userId) : // 添加 userId 参数
QWidget(parent),
ui(new Ui::UsersWidget),
userId(userId) // 初始化 userId 成员变量
{
ui->setupUi(this);
displayUsersWidget();
connect(ui->lights,&QToolButton::clicked,this,[=]{
Lights *w = new Lights(this, userId); // 传递 userId 给 Lights
w->show();});
connect(ui->air_condition,&QToolButton::clicked,this,[=]{
AirCondition *w = new AirCondition;
w->show();});
connect(ui->curtains,&QToolButton::clicked,this,[=]{
Curtains *w = new Curtains;
w->show();});
connect(ui->humidifiers,&QToolButton::clicked,this,[=]{
Humidiffer *w = new Humidiffer;
w->show();});
connect(ui->tables,&QToolButton::clicked,this,[=]{
Information *w = new Information;
w->show();});
connect(ui->wifi,&QToolButton::clicked,this,[=]{
Procession *w = new Procession;
w->show();});
}
UsersWidget::~UsersWidget()
{
delete ui;
}
void UsersWidget::displayUsersWidget()
{
setWindowTitle("智能家居总览图");
setFixedSize(900,600);
}
void UsersWidget::closeEvent(QCloseEvent *e)
{
if(e->spontaneous())
{
//窗口关闭时弹出的提示窗口
QMessageBox::StandardButton reply;
reply = QMessageBox::information(this,"提示","确认退出智能管家系统?",QMessageBox::Yes|QMessageBox::No);
if(reply == QMessageBox::Yes){
//若用户点击确认,则接收这个事件,当前窗口会关闭
e->accept();
}else{
//若用户点击取消,则忽略这个事件,当前窗口不会关闭
e->ignore();
}
}
}
#ifndef LIGHTS_H
#define LIGHTS_H
#include <QWidget>
namespace Ui {
class Lights;
}
class Lights : public QWidget
{
Q_OBJECT
public:
explicit Lights(QWidget *parent = nullptr, int userId = 0); // 添加 userId 参数
~Lights();
void LightsWidget();
bool flag;
public slots:
bool switchButton_status(bool checked);
private:
Ui::Lights *ui;
int userId; // 添加 userId 成员变量
};
#endif // LIGHTS_H
#include "lights.h"
#include "ui_lights.h"
#include "switchform.h"
#include "sqlite.h"
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
Lights::Lights(QWidget *parent, int userId) : // 添加 userId 参数
QWidget(parent),
ui(new Ui::Lights),
userId(userId) // 初始化 userId 成员变量
{
ui->setupUi(this);
LightsWidget();
//连接开关组件的信号和自定义的槽函数
connect(ui->SwitchStatus,&SwitchForm::statusChanged,this,&Lights::switchButton_status);
// 连接完成按钮的clicked信号和自定义的槽函数
connect(ui->Finished, &QPushButton::clicked, this,[=]{
if(flag)
{
QString brighteness = QString::number(ui->ctrlform->getValue());
qDebug()<<brighteness;
Sqlite::sqlite_init();
updateSmartHomeStatus(userId, "智能灯亮度", brighteness); // 使用 userId 更新数据
}
});
}
Lights::~Lights()
{
delete ui;
}
void Lights::LightsWidget()
{
setWindowTitle("智能灯设置");
//设置ui组件背景
setAutoFillBackground(true);
QPalette palette=this->palette();
QPixmap pixmap(":/user/image/image/light.jpg");
palette.setBrush(QPalette::Window, QBrush(pixmap));
setPalette(palette);
setFixedSize(600,400);
}
bool Lights::switchButton_status(bool checked)
{
qDebug()<<checked;
flag=checked;
return flag;
}
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
#include "sqlite.h"
Sqlite::Sqlite()
{
}
void Sqlite::sqlite_init()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("../Smarthome_Client/database/database.db");
if(!db.open())
{
qDebug() << "无法打开数据库!";
}
else{
qDebug() << "数据库打开成功!";
}
}
// 更新智能家居状态表中的数据
void updateSmartHomeStatus(const int userId, const QString& deviceName, const QString& newValue)
{
Sqlite::sqlite_init();
// 执行更新操作
QSqlQuery query;
QString sql = QString("UPDATE status SET %1 = :newValue WHERE user_id = :userId")
.arg(deviceName);
query.prepare(sql);
query.bindValue(":newValue", newValue);
query.bindValue(":userId", userId);
if (!query.exec()) {
qDebug() << "更新数据失败:" << query.lastError().text();
} else {
qDebug() << "数据更新成功";
}
}
void StatusUid(int userId)
{
Sqlite::sqlite_init();
QSqlQuery query;
// 查询智能家居状态表,获取用户对应的状态
query.prepare("SELECT * FROM status WHERE user_id = :userId");
query.bindValue(":userId", userId);
qDebug() << query.exec();
if (!query.next()) {
qDebug() << "Failed to execute query!" << query.lastError().text();
}
}
在 Lights 界面中使用 userId:
- 传递 userId: 在
Manage中,登录成功后,获取用户 ID 并将其作为参数传递给UsersWidget的构造函数。 - 保存 userId: 在
UsersWidget的构造函数中,定义一个成员变量userId来保存接收到的 ID。 - 继续传递 userId: 在
UsersWidget中,当用户点击按钮生成Lights界面时,将userId作为参数传递给Lights的构造函数。 - 使用 userId: 在
Lights的构造函数中,定义一个成员变量userId来保存接收到的 ID,并在更新数据库状态时使用该 ID。
更新代码示例:
// Manage.cpp
// ...
void Manage::loginUser(QString username,QString passwd)
{
// ...
int userId = query.value("uid").toInt();
StatusUid(userId);
// ...
UsersWidget *w = new UsersWidget(userId); // 传递 userId 给 UsersWidget
// ...
}
// UsersWidget.cpp
// ...
UsersWidget::UsersWidget(QWidget *parent, int userId) :
// ...
userId(userId) // 初始化 userId 成员变量
{
// ...
connect(ui->lights,&QToolButton::clicked,this,[=]{
Lights *w = new Lights(this, userId); // 传递 userId 给 Lights
// ...
});
// ...
}
// Lights.cpp
// ...
Lights::Lights(QWidget *parent, int userId) :
// ...
userId(userId) // 初始化 userId 成员变量
{
// ...
connect(ui->Finished, &QPushButton::clicked, this,[=]{
// ...
updateSmartHomeStatus(userId, "智能灯亮度", brighteness); // 使用 userId 更新数据
// ...
});
// ...
}
通过以上步骤,您就可以在 Lights 界面中使用 Manage 获取到的 userId 来更新智能家居状态表的数据,实现用户与智能家居状态之间的关联。
注意:
- 以上代码示例仅供参考,实际代码需要根据您的具体需求进行修改。
- 请确保您的数据库已正确配置,并且您已创建了
users和status表。 - 在
Lights界面中,您还需要根据实际需求编写代码来获取智能灯的当前状态并显示在界面上。
总结:
本项目提供了一个使用 Qt C++ 开发智能家居系统的基本框架,并演示了如何使用 SQLite 数据库来存储用户信息和智能家居状态。您可以根据自己的需求扩展功能和界面,例如添加更多智能设备、支持不同模式的切换、实现远程控制等。
希望本项目能帮助您入门 Qt C++ 智能家居开发,并激发您进一步探索和创新的灵感。
原文地址: https://www.cveoy.top/t/topic/fAti 著作权归作者所有。请勿转载和采集!