void MainWindow::moveAgvs_(){ timer =new QTimer(this); timer->start(1000); connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);} void MainWindow::moveAgvs() { Astar astar; std::vector<Node*> path; std::vector<std::vector<Node*>> paths(agvs.size()); // 得到agv的路綫 for (int i = 0; i < agvs.size(); i++) { Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node; if (agvs[i].getLoad() == false) { end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY()); } else { end_node = new Node(agvs[i].getEndX(), agvs[i].getEndY()); } std::vector<Node*> path = astar.getPath(start_node, end_node); paths[i] = path; //輸出agv的路綫 std::cout << "AGV " << i << " path: "; for (int j = 0; j < path.size(); j++) { std::cout << "(" << path[j]->x << ", " <<path[j]->y << ")"; if (j != path.size() - 1) { std::cout << " -> "; } } 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; QTimer::singleShot(time, this, [&, i, next_node]() { agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); std::cout << "AGV " << agvs[i].getid() << " current_x: " << agvs[i].getCurrentX() << " current_y: " << agvs[i].getCurrentY() <<std::endl; this->update(); if (next_node->x == agvs[i].getEndX() && next_node->y == agvs[i].getEndY()) { //task_to_agv(i); } }); } } },沒有運行報錯
时间: 2023-07-16 07:14:59 浏览: 67
Qt_Timer:用QT实现的计时器
根据您提供的代码,我看到您定义了两个函数 `moveAgvs_` 和 `moveAgvs`。您连接了一个 `timer` 对象的 `timeout` 信号到 `moveAgvs` 槽函数,但是在 `moveAgvs_` 函数中没有定义 `timer` 对象。我猜测您可能是想在 `moveAgvs_` 函数中创建 `timer` 对象并启动它,再将它的 `timeout` 信号连接到 `moveAgvs` 槽函数。所以您可以将 `moveAgvs_` 函数修改为以下代码:
```
void MainWindow::moveAgvs_()
{
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);
timer->start(1000);
}
```
这样,当 `moveAgvs_` 函数被调用时,会创建一个 `timer` 对象并启动它,同时将其 `timeout` 信号连接到 `moveAgvs` 槽函数。希望这能帮到您解决问题。
阅读全文