在UsersWidget类中,添加一个私有成员变量userid,用于保存用户的ID。在构造函数中将传入的userid赋值给成员变量this->userid

UsersWidget类的构造函数中,连接QComboBoxcurrentIndexChanged信号到槽函数on_modeComboBox_currentIndexChanged。在槽函数中根据选择的模式更新智能家居的状态。

Lights类的构造函数中,添加一个私有成员变量userid,用于保存用户的ID。在构造函数中将传入的userid赋值给成员变量this->userid

Lights类的构造函数中,连接SwitchFormstatusChanged信号到槽函数switchButton_status。在槽函数中根据开关的状态更新智能灯的状态。

Lights类的构造函数中,连接QPushButtonclicked信号到槽函数,用于保存更改后的状态。

Lights类的showEvent函数中,调用loadLastStatus函数,加载上次更改的状态。

Lights类的hideEvent函数中,调用saveLastStatus函数,保存当前状态。

Lights类的closeEvent函数中,隐藏窗口而不是关闭。

添加Lights类的私有函数loadLastStatus,用于加载上次更改的状态。在函数中根据用户ID查询数据库,获取上次的状态和亮度,并根据查询结果更新界面上的状态和亮度。

添加Lights类的私有函数saveLastStatus,用于保存当前状态。在函数中获取界面上的亮度,并将其保存到数据库中。

添加Lights类的私有函数getLastBrightness,用于查询上次的亮度。在函数中根据用户ID查询数据库,获取上次的亮度。

添加Lights类的私有函数updateLastBrightness,用于更新上次的亮度。在函数中根据用户ID更新数据库中的亮度。

添加Lights类的私有函数getLastState,用于查询上次的状态。在函数中根据用户ID查询数据库,获取上次的状态。

完整的代码如下所示:

#include "userswidget.h"
#include "ui_userswidget.h"
#include "sqlite.h"
#include "lights.h"
#include "aircondition.h"
#include "curtains.h"
#include "humidiffer.h"
#include "information.h"
#include "procession.h"
#include "QCloseEvent"
#include "QComboBox"
#include "QMessageBox"

UsersWidget::UsersWidget(int userid,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UsersWidget),
    userid(userid)
{
    ui->setupUi(this);
    displayUsersWidget();
    connect(ui->lights,&QToolButton::clicked,this,[=]{
        Lights *w = new Lights(userid);
        w->show();});
    connect(ui->air_condition,&QToolButton::clicked,this,[=]{
        AirCondition *w = new AirCondition(userid);
        w->show();});
    connect(ui->curtains,&QToolButton::clicked,this,[=]{
        Curtains *w = new Curtains(userid);
        w->show();});
    connect(ui->humidifiers,&QToolButton::clicked,this,[=]{
        Humidiffer *w = new Humidiffer(userid);
        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();});
    connect(ui->mode, QOverload<const QString&>::of(&QComboBox::currentIndexChanged), this, [this](const QString& mode){
        on_modeComboBox_currentIndexChanged(mode);
    });
}

UsersWidget::~UsersWidget()
{
    delete ui;
}

void UsersWidget::displayUsersWidget()
{
    setWindowTitle("智能家居总览图");
    setFixedSize(900,600);
}

void UsersWidget::on_modeComboBox_currentIndexChanged(const QString &mode)
{
    if (mode == "睡眠模式") {
        updateSmartHomeStatus(userid, "智能灯", "开启", "20");
        updateSmartHomeStatus(userid, "空调", "开启", "27");
        updateSmartHomeStatus(userid, "加湿器", "开启", "50");
        updateSmartHomeStatus(userid, "窗帘", "开启", "100");
    } else if (mode == "日常模式") {
        updateSmartHomeStatus(userid, "智能灯", "开启", "80");
        updateSmartHomeStatus(userid, "空调", "开启", "25");
        updateSmartHomeStatus(userid, "加湿器", "开启", "60");
        updateSmartHomeStatus(userid, "窗帘", "开启", "50");
    } else if (mode == "节能模式") {
        updateSmartHomeStatus(userid, "智能灯", "开启", "50");
        updateSmartHomeStatus(userid, "空调", "开启", "26");
        updateSmartHomeStatus(userid, "加湿器", "开启", "40");
        updateSmartHomeStatus(userid, "窗帘", "开启", "30");
    }
}

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();
        }
    }
}

#include "lights.h"
#include "ui_lights.h"
#include "switchform.h"
#include "sqlite.h"
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include <QCloseEvent>
#include <QDebug>

Lights::Lights(int userid,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Lights),
    userid(userid)
{
    ui->setupUi(this);
    lightsWidget();
    connect(ui->SwitchStatus,&SwitchForm::statusChanged,this,&Lights::switchButton_status);
    connect(ui->Finished, &QPushButton::clicked, this,[=]{
        if(ui->SwitchStatus->checked())
        {
            QString brighteness = QString::number(ui->ctrlform->getValue());
            qDebug()<<brighteness<<userid;
            updateSmartHomeStatus(userid,QString("智能灯"),QString("开启"),brighteness);
        }else
        {
            qDebug()<<"智能灯未打开!";
            updateSmartHomeStatus(userid,QString("智能灯"),QString("关闭"),QString("0"));
        }
    });
    loadLastStatus();
}

Lights::~Lights()
{
    delete ui;
}

void Lights::lightsWidget()
{
    setWindowTitle("智能灯设置");
    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;
}

void Lights::showEvent(QShowEvent *event)
{
    loadLastStatus();
    event->accept();
}

void Lights::hideEvent(QHideEvent *event)
{
    saveLastStatus();
    event->accept();
}

void Lights::closeEvent(QCloseEvent *event)
{
    hide();
    event->ignore();
}

void Lights::loadLastStatus()
{
    qDebug()<<this->userid;
    QString state = getLastState(this->userid);
    QString brightness = getLastBrightness(this->userid);
    qDebug()<<brightness;
    if (state=="开启")
    {
        ui->SwitchStatus->setChecked(true);
        ui->ctrlform->setValue(brightness.toInt());
    }
    else
    {
        ui->Switc
代码补充:要求用户在UserWidget界面点击QComboBox选择模式SQlite的Status表内数据改变点击Lights界面数据同时被更新#include userswidgeth#include ui_userswidgeth#include sqliteh#include lightsh#include airconditionh#include curtainsh#include h

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

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