void MainWindow::moveAgvs_(){ timer =new QTimer(this); timer->start(100); connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);} void MainWindow::moveAgvs() { Astar astar; std::vector<std::vector<Node*>> paths(agvs.size()); // 得到agv的路綫 for (int i = 0; i < agvs.size(); i++) { if (agvs[i].getState() == false) { if (agvs[i].getLoad()){ //如果是負載的狀態,則任務的起點到任務的終點 if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { agvs[i].setState(true); agvs[i].setLoad(false); tasks[i].setCompleted(2); task_to_agv(); } Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY()); std::vector<Node*> path_to_end = astar.getPath(start_node, end_node1); path_to_end.erase(path_to_end.begin()); std::vector<Node*> path; path.insert(path.end(), path_to_end.begin(), path_to_end.end()); paths[i] = path; } else { //如果是空載的狀態,則行駛到任務的起點 //如果agv已經到達任務起點,變爲負載狀態 if (agvs[i].getCurrentX() == agvs[i].getStartX() && agvs[i].getCurrentY() == agvs[i].getStartY()) { agvs[i].setLoad(true); } Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY()); std::vector<Node*> path_to_start = astar.getPath(start_node, end_node); std::vector<Node*> path; path.insert(path.end(), path_to_start.begin() + 1, path_to_start.end()); paths[i] = path; } } for (int i = 0; i < agvs.size(); i++) { std::cout << "path of AGV " << i << ": "; for (int j = 0; j < paths[i].size(); j++) { std::cout << "(" << paths[i][j]->x << ", " << paths[i][j]->y << ") "; } std::cout << std::endl; } //模擬小車行駛 for (int i = 0; i < agvs.size(); i++) { if (! paths[i].empty()) { Node* next_node = paths[i][0]; float speed = agvs[i].getSpeed(); float distance = sqrt(pow(next_node->x - agvs[i].getCurrentX(), 2) + pow(next_node->y - agvs[i].getCurrentY(), 2)); float time = distance / speed * 1000; //node_Value[next_node->x][next_node->y] = 10; QTimer::singleShot(time, this, &, i, next_node { agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); this->update(); // 在窗口中重绘 }); } } } },添加代碼:黨所有任務的completed為2時,結束qtime
时间: 2023-12-06 13:45:45 浏览: 27
你可以在moveAgvs函数中添加如下代码来检测所有任务的completed属性是否都为2,如果是,则停止QTimer:
```
bool allTasksCompleted = true;
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].getCompleted() != 2) {
allTasksCompleted = false;
break;
}
}
if (allTasksCompleted) {
timer->stop();
}
```
这段代码遍历所有任务,如果有任务的completed属性不为2,则将allTasksCompleted设置为false,否则一直为true。如果allTasksCompleted为true,说明所有任务都已完成,此时停止QTimer即可。
相关问题
#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")); } }错在哪
代码存在一些问题:
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"));
}
}
```
#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的数据逐渐增大,而不是随意乱弹出数据
您可以使用一个变量来保存当前的时间,每次更新温度时增加这个变量的值,然后用变量的值作为计时器的间隔。具体来说,您需要做以下更改:
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);
}
```
阅读全文