修改后的代码如下:

controlform.h:

#ifndef CONTROLFORM_H
#define CONTROLFORM_H

#include <QWidget>

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的值

};

#endif // CONTROLFORM_H

controlform.cpp:

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

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

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

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

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

lights.h:

#ifndef LIGHTS_H
#define LIGHTS_H

#include <QWidget>

namespace Ui {
class Lights;
}

class Lights : public QWidget
{
    Q_OBJECT

public:
    explicit Lights(int userid, QWidget *parent = nullptr);
    ~Lights();
    void lightsWidget();
    bool flag=false;
    void loadLastStatus(); // 加载上次更改的状态
    void saveLastStatus(); // 保存当前更改的状态

public slots:
    bool switchButton_status(bool checked);
    void showEvent(QShowEvent *event); // 重写showEvent函数

private:
    Ui::Lights *ui;
    int userid;
    void closeEvent(QCloseEvent *event);
};

#endif // LIGHTS_H

lights.cpp:

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

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

bool Lights::switchButton_status(bool checked)
{
    qDebug() << checked;
    flag = checked;
    return flag;
}

void Lights::showEvent(QShowEvent *event)
{
    // 当页面显示时,加载上次更改的状态
    loadLastStatus();
    event->accept();
}

void Lights::closeEvent(QCloseEvent *event)
{
    // 隐藏窗口而不是关闭
    hide();
    event->ignore();
}

void Lights::loadLastStatus()
{
    // 加载上次更改的状态
    // 根据userid从数据库中获取上次更改的状态,并根据状态更新界面
    // 示例代码:
    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);
}
``
修改代码实现一些功能:ui-ctrlform-setValue0中ctrlform没有setValue0不能使用这条语句Qt 界面点击关闭按钮隐藏起来等到再次进入这个页面页面上的内容为上次更改的状态#ifndef CONTROLFORM_H#define CONTROLFORM_H#include QWidgetnamespace Ui class ControlForm;class Contro

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

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