#include "controlform.h" #include "ui_controlform.h"

ControlForm::ControlForm(QWidget *parent) : QWidget(parent), ui(new Ui::ControlForm) { ui->setupUi(this);

//完成信号和槽的关联
//Qt5实现
connect(ui->horizontalSlider,&QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);

//void valueChanged(int i)  信号的原始声明
//void valueChanged(const QString &text)
void (QSpinBox::*vci)(int) = &QSpinBox::valueChanged;//函数指针
connect(ui->spinBox,vci,ui->horizontalSlider,&QSlider::setValue);

}

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

int ControlForm::getValue()const { return ui->spinBox->value(); }

void ControlForm::setValue(int value) { ui->spinBox->setValue(value); }

#include "lights.h" #include "ui_lights.h" #include "switchform.h" #include "sqlite.h" #include #include #include #include #include #include

Lights::Lights(int userid,QWidget *parent) : QWidget(parent), ui(new Ui::Lights) { ui->setupUi(this); lightsWidget(); //连接开关组件的信号和自定义的槽函数 connect(ui->SwitchStatus,&SwitchForm::statusChanged,this,&Lights::switchButton_status);

// 连接完成按钮的clicked信号和自定义的槽函数
connect(ui->Finished, &QPushButton::clicked, this,[=]{
    if(flag)
    {
        QString brightness = QString::number(ui->ctrlform->getValue());
        qDebug()<<brightness;
        updateLastBrightness(userid,QString("智能灯"),QString("开启"),brightness);
    }else
    {
        QMessageBox::information(this,"提示","智能灯未打开!");
        qDebug()<<"智能灯未打开!";
        updateLastBrightness(userid,QString("智能灯"),QString("关闭"),QString("0"));
    }
});
// 加载上次更改的状态
    loadLastStatus();

}

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

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() { QString brightness = getLastBrightness(userid); if (brightness.toInt() > 0) { ui->SwitchStatus->setChecked(true); ui->ctrlform->setValue(brightness.toInt()); } else { ui->SwitchStatus->setChecked(false); ui->ctrlform->setValue(0); } }

void Lights::saveLastStatus() { QString brightness = QString::number(ui->ctrlform->getValue()); updateLastBrightness(userid, brightness); }

QString Lights::getLastBrightness(int userid) { Sqlite::sqlite_init(); QString queryStr = QString("SELECT value FROM Status WHERE uid = %1 AND device_name = '智能灯'").arg(userid); QSqlQuery query; if (query.exec(queryStr) && query.next()) { return query.value(0).toString(); } else { qDebug() << "得到上一次亮度错误: " << query.lastError().text(); return ""; } }

void Lights::updateLastBrightness(int userid,QString brightness) { Sqlite::sqlite_init(); QString queryStr = QString("UPDATE Status SET value = '%1' WHERE uid = %2 AND device_name = '智能灯'").arg(brightness).arg(userid); QSqlQuery query; if (!query.exec(queryStr)) { qDebug() << "更新亮度失败: " << query.lastError().text(); }

改错:#ifndef CONTROLFORM_H#define CONTROLFORM_H#include QWidgetnamespace Ui class ControlForm;class ControlForm public QWidget Q_OBJECTpublic explicit ControlFormQWidget parent = nullptr; ~Con

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

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