#include "mainwindow.h"#include "ui_mainwindow.h"#include <QMessageBox>#include <QTextEdit>#include <QPushButton>TemperatureThread::TemperatureThread(QObject *parent) : QThread(parent){ m_temperature = 0.0;}void TemperatureThread::run(){ while (true){ m_temperature = qrand() % 100; emit temperatureChanged(m_temperature); msleep(1000); }}MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); m_temperatureThread = new TemperatureThread(this); m_timer = new QTimer(this); m_threshold = 0.0; connect(m_temperatureThread, &TemperatureThread::temperatureChanged, this, &MainWindow::updateTemperature);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_startButton_clicked(){ if (m_temperatureThread->isRunning()) { QMessageBox::warning(this,tr("Warning"),tr("Alarm already started")); return; } bool ok; m_threshold = ui->thresholdEdit->text().toDouble(&ok); if (!ok) { QMessageBox::warning(this,tr("warning"),tr("Invalid")); return; } m_temperatureThread->start(); m_timer->start(1000);}void MainWindow::on_stopButton_clicked(){ if (!m_timer->isActive() || !m_temperatureThread->isRunning()) { QMessageBox::warning(this,tr("Warning"),tr("Alarm not started yet")); return; } m_temperatureThread->quit(); m_temperatureThread->wait(); m_timer->stop(); ui->temperatureLabel->setText(QString::number(0.0));}void MainWindow::updateTemperature(double temperature){ ui->temperatureLabel->setText(QString::number(temperature)); if (temperature > m_threshold){ QMessageBox::critical(this,tr("Warning"),tr("Temperature too high")); }}这段代码怎么改,可以使timer的数据逐渐增大,而不是随意乱弹出数据
时间: 2023-12-10 11:38:21 浏览: 142
您可以使用一个变量来保存当前的时间,每次更新温度时增加这个变量的值,然后用变量的值作为计时器的间隔。具体来说,您需要做以下更改:
1.在MainWindow类中添加一个私有变量m_time,用于保存当前时间。
2.在MainWindow类的构造函数中将m_time初始化为0。
3.在MainWindow类的updateTemperature函数中增加m_time的值。
4.在MainWindow类的on_startButton_clicked函数中,将计时器的间隔设置为m_time的值,并在开始计时器之前将m_time的值增加。
5.在MainWindow类的on_stopButton_clicked函数中,停止计时器时将m_time重置为0。
下面是修改后的代码:
```
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QTextEdit>
#include <QPushButton>
TemperatureThread::TemperatureThread(QObject *parent)
: QThread(parent){
m_temperature = 0.0;
}
void TemperatureThread::run(){
while (true){
m_temperature = qrand() % 100;
emit temperatureChanged(m_temperature);
msleep(1000);
}
}
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
m_temperatureThread = new TemperatureThread(this);
m_timer = new QTimer(this);
m_threshold = 0.0;
m_time = 0;
connect(m_temperatureThread, &TemperatureThread::temperatureChanged,
this, &MainWindow::updateTemperature);
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::on_startButton_clicked(){
if (m_temperatureThread->isRunning()) {
QMessageBox::warning(this,tr("Warning"),tr("Alarm already started"));
return;
}
bool ok;
m_threshold = ui->thresholdEdit->text().toDouble(&ok);
if (!ok) {
QMessageBox::warning(this,tr("warning"),tr("Invalid"));
return;
}
m_time = 0;
m_timer->start(m_time);
m_temperatureThread->start();
}
void MainWindow::on_stopButton_clicked(){
if (!m_timer->isActive() || !m_temperatureThread->isRunning()) {
QMessageBox::warning(this,tr("Warning"),tr("Alarm not started yet"));
return;
}
m_temperatureThread->quit();
m_temperatureThread->wait();
m_timer->stop();
ui->temperatureLabel->setText(QString::number(0.0));
m_time = 0;
}
void MainWindow::updateTemperature(double temperature){
ui->temperatureLabel->setText(QString::number(temperature));
m_time += 1000;
if (temperature > m_threshold){
QMessageBox::critical(this,tr("Warning"),tr("Temperature too high"));
}
m_timer->setInterval(m_time);
}
```
阅读全文