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(); // 在窗口中重绘 })添加代碼,等qtime停止的時候,輸出“wsn“
时间: 2024-02-07 14:04:19 浏览: 50
您可以使用 QTimer 的 finished() 信号来检测所有 QTimer 是否都已经停止,并在所有 QTimer 停止后输出 "wsn"。
以下是示例代码:
```
// 创建 QTimer 对象并连接到槽函数
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [=]() {
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 的数量
int timer_count = 5;
// 连接到所有 QTimer 的 finished() 信号
connect(timer, &QTimer::finished, this, [=]() {
timer_count--;
if (timer_count == 0) {
std::cout << "wsn" << std::endl;
}
});
// 启动 QTimer
timer->setSingleShot(true);
timer->start(time);
```
请注意,上面的示例代码中计数器的初始值为 5,这意味着您需要手动将计数器的初始值设置为您实际使用的 QTimer 的数量。
阅读全文