Qt智能灯控制程序设计与实现
#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); // 新增的槽函数,用于设置spinBox的值
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); // 重写showEvent函数
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);
//完成信号和槽的关联
//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
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;
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('智能灯设置'); //设置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); }
boolean 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/fAPH 著作权归作者所有。请勿转载和采集!