Qt C++ 智能家居系统开发:用户管理和状态同步

本项目使用 Qt C++ 开发了一个智能家居系统,包含用户管理和智能家居设备状态控制功能,并利用 SQLite 数据库进行数据存储和同步。

系统架构

  • 数据库: SQLite 数据库用于存储用户数据和智能家居设备状态。
  • 用户表: 存储用户的用户名和密码。
  • 智能家居状态表: 记录智能灯开关、亮度、空调开关、温度、加湿器开关、湿度、窗帘开关、高度、模式信息(睡眠模式、日常模式、节能模式)等。
  • 客户端: Qt C++ 应用程序作为客户端,实现用户登录、注册和智能家居设备状态控制。

核心功能

  1. 用户登录与注册:
    • 用户在登录界面输入用户名和密码进行登录。
    • 如果用户不存在,则跳转到注册界面进行注册。
  2. 智能家居状态控制:
    • 登录成功后,用户可以进入智能家居控制界面,分别对智能灯、空调、加湿器、窗帘和模式进行操作。
    • 用户对设备状态的更改会通过 updateSmartHomeStatus() 函数同步到数据库中。

代码示例

#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;
private:
    int userId; // 保存userId
    Ui::Lights *ui;
};

#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, "light", 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();
    }
}

Manage 类中,将 userId 传递给 UsersWidget 的构造函数:

UsersWidget *w = new UsersWidget(nullptr, userId); // 将userId传递给UsersWidget

UsersWidget 类中,将 userId 传递给 Lights 的构造函数:

Lights *w = new Lights(this, userId); // 将userId传递给Lights

Lights 类中,使用 userId 更新数据库:

updateSmartHomeStatus(userId, "light", brighteness); // 使用userId更新数据

通过以上修改, Lights 界面就可以使用 Manage 获取到的 userId 信息,从而实现用户和设备状态的关联。

Qt C++ 智能家居系统开发:用户管理和状态同步

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

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