智能灯设置:加载上次亮度错误
#ifndef CONTROLFORM_H #define CONTROLFORM_H
#include
namespace Ui { class ControlForm; }
class ControlForm : public QWidget { Q_OBJECT
public: explicit ControlForm(QWidget *parent = nullptr); ~ControlForm(); int getValue() const ;
private: Ui::ControlForm *ui;
public slots: void setValue(int value); signals: void valueChanged(int value); };
#endif // CONTROLFORM_H
#ifndef LIGHTS_H #define LIGHTS_H
#include
namespace Ui { class Lights; }
class Lights : public QWidget { Q_OBJECT
public: explicit Lights(int userid, QWidget *parent = nullptr); ~Lights(); void lightsWidget(); bool flag = false;
bool switchButton_status(bool checked);
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
void loadLastStatus();
void saveLastStatus();
private: Ui::Lights *ui; int userid; void closeEvent(QCloseEvent *event); QString getLastBrightness(int userid); void updateLastBrightness(int userid, QString brightness); };
#endif // LIGHTS_H
#include "controlform.h" #include "ui_controlform.h"
ControlForm::ControlForm(QWidget *parent) : QWidget(parent), ui(new Ui::ControlForm) { ui->setupUi(this);
connect(ui->horizontalSlider, &QSlider::valueChanged, ui->spinBox, &QSpinBox::setValue);
connect(ui->spinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), 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
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);
connect(ui->Finished, &QPushButton::clicked, this, [=]{
if (flag)
{
QString brightness = QString::number(ui->ctrlform->getValue());
qDebug() << brightness;
updateSmartHomeStatus(userid, QString('智能灯'), QString('开启'), brightness);
}
else
{
QMessageBox::information(this, "提示", "智能灯未打开!");
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() { 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(); } }
原文地址: http://www.cveoy.top/t/topic/fAPR 著作权归作者所有。请勿转载和采集!