void MainWindow::moveAgvs_(){ timer =new QTimer(this); timer->start(1000); connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs); }, QTimer::singleShot(time, this, [&, i, next_node, cur_x, cur_y, power_consumption, j, steps]() { float ratio = static_cast<float>(j) / steps; double new_x = cur_x + ratio * (next_node->x - cur_x); double new_y = cur_y + ratio * (next_node->y - cur_y); agvs[i].setCurrentX(new_x); agvs[i].setCurrentY(new_y); std:: cout <<"AGV"<<"("<< new_x << "," << new_y << ")" << endl; // 更新电量 agvs[i].setpower(agvs[i].power_ - power_consumption / steps); this->update(); // 在窗口中重绘 });,修改代碼為:第二个 QTimer 结束后再去运行第一个 QTimer
时间: 2024-01-28 16:02:45 浏览: 40
你可以在第二个 QTimer 的 timeout 信号中启动第一个 QTimer。例如:
```c++
void MainWindow::myTimeout() {
// 处理第二个 QTimer 的 timeout 信号
...
// 第二个 QTimer 完成之后,启动第一个 QTimer
QTimer* timer = new QTimer(this);
timer->start(1000);
connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs_);
}
```
然后在你的代码中使用 QTimer::singleShot() 启动第二个 QTimer,并指定时间 time 为第二个 QTimer 的触发时间,如下所示:
```c++
QTimer::singleShot(time, this, [&, i, next_node, cur_x, cur_y, power_consumption, j, steps]() {
float ratio = static_cast<float>(j) / steps;
double new_x = cur_x + ratio * (next_node->x - cur_x);
double new_y = cur_y + ratio * (next_node->y - cur_y);
agvs[i].setCurrentX(new_x);
agvs[i].setCurrentY(new_y);
std:: cout <<"AGV"<<"("<< new_x << "," << new_y << ")" << endl;
// 更新电量
agvs[i].setpower(agvs[i].power_ - power_consumption / steps);
this->update(); // 在窗口中重绘
// 第二个 QTimer 完成之后,启动第一个 QTimer
QTimer* timer = new QTimer(this);
timer->start(1000);
connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs_);
});
```
这样,第二个 QTimer 完成之后,会执行 []() 中的 lambda 表达式,并在其中启动第一个 QTimer,从而实现第二个 QTimer 结束后再去运行第一个 QTimer。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)