#include "mainwindow.h" #include "ui_mainwindow.h" #include<QMessageBox> #include<QTextEdit> #include<QPushButton> TemperatureThread::TemperatureThread(QObject *parent) : QThread(parent) { m_temperature = 0.0; QPushButton * btn= new QPushButton(); //btn->show(); btn->setText("this"); btn->setParent(QObject); } 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_timer->isActive()) { 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()){ QMessageBox::warning(this,tr("Warning"),tr("Alarm not started yet")); return; } m_temperatureThread->quit(); m_temperatureThread->wait(); m_timer->stop(); ui->temperatureLabel->setText(tr("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")); } }错在哪
时间: 2023-12-03 18:46:33 浏览: 160
moc.exe 解决moc过程中不支持处理enum包含#include头文件的语法。
5星 · 资源好评率100%
代码存在一些问题:
1. 在 TemperatureThread 的构造函数中创建了一个 QPushButton 实例,但并没有将其添加到界面中。
2. 在创建 QPushButton 实例时,使用了 QObject 作为其父对象,应该使用 MainWindow 或其子类作为父对象。
3. 在 on_startButton_clicked 函数中,应该先判断是否已经启动了温度线程,再进行阈值的设置和启动定时器。
4. 在 on_stopButton_clicked 函数中,应该先判断是否已经启动了定时器和温度线程,再停止定时器和温度线程,并将温度显示标签的文本清零。
5. 在 updateTemperature 函数中,应该将温度值转换为字符串后再设置到温度显示标签上。
下面是修改后的代码:
```c++
#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"));
}
}
```
阅读全文