for (int i = 0; i < agvs.size(); i++) { if (agvs[i].getLoad() == true) { // 如果是负载的状态 if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { // 如果到达终点 agvs[i].setLoad(false); // 设置为空载状态 agvs[i].setState(true); std::cout << "agv__id :" << agvs[i].getid() << " ,agv_get_task_id :" << agvs[i].get_task_id() << endl; for (int j = 0; j < tasks.size(); j++) { if (tasks[j].id == agvs[i].get_task_id()) { completed_task_index = j; break; } } if (completed_task_index != -1) { tasks[completed_task_index].completed = 2; } task_to_agv(); // 更新任务分配 update(); // 更新AGV状态 } else { // 否则行驶到终点 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 = astar.getPath(start_node, end_node1); path.erase(path.begin()); paths[i] = path; } } else { // 如果是空载的状态 if (agvs[i].getCurrentX() == agvs[i].getStartX() && agvs[i].getCurrentY() == agvs[i].getStartY()) { // 如果到达起点 agvs[i].setLoad(true); // 设置为负载状态 } else { // 否则行驶到起点 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 = astar.getPath(start_node, end_node); path.erase(path.begin()); paths[i] = path; } } }, if (!paths[i].empty()) { QPainter painter(this); painter.setPen(QPen(Qt::red, 5)); // 设置画笔颜色和宽度 for (int j = 0; j < paths[i].size() - 1; j++) { // 遍历每个子数组中的点 QPoint start(paths[i][j]->x * 25 + 200, paths[i][j]->y * 25 + 50); QPoint end(paths[i][j + 1]->x * 25 + 200, paths[i][j + 1]->y * 25 + 50); painter.drawLine(start, end); } },一運行繪製path路綫,就白屏卡死,怎麽修改代碼
时间: 2024-01-20 09:03:58 浏览: 57
这段代码中可能存在一些问题,导致白屏卡死。你可以尝试以下修改:
1.在绘制路径之前,先判断一下 `paths[i]` 是否为空,如果是空的话就不进行绘制,避免出现白屏。
```
if (!paths[i].empty()) {
QPainter painter(this);
painter.setPen(QPen(Qt::red, 5)); // 设置画笔颜色和宽度
for (int j = 0; j < paths[i].size() - 1; j++) { // 遍历每个子数组中的点
QPoint start(paths[i][j]->x * 25 + 200, paths[i][j]->y * 25 + 50);
QPoint end(paths[i][j + 1]->x * 25 + 200, paths[i][j + 1]->y * 25 + 50);
painter.drawLine(start, end);
}
}
```
2.在更新 AGV 状态之前,先判断一下 `completed_task_index` 是否为 -1,如果是 -1 的话就不进行任务完成状态的更新,避免出现数组越界的问题。
```
if (agvs[i].getLoad() == true) { // 如果是负载的状态
if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { // 如果到达终点
agvs[i].setLoad(false); // 设置为空载状态
agvs[i].setState(true);
std::cout << "agv__id :" << agvs[i].getid() << " ,agv_get_task_id :" << agvs[i].get_task_id() << endl;
for (int j = 0; j < tasks.size(); j++) {
if (tasks[j].id == agvs[i].get_task_id()) {
completed_task_index = j;
break;
}
}
if (completed_task_index != -1) {
tasks[completed_task_index].completed = 2;
}
task_to_agv(); // 更新任务分配
update(); // 更新AGV状态
} else {
// 否则行驶到终点
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 = astar.getPath(start_node, end_node1);
path.erase(path.begin());
paths[i] = path;
}
} else { // 如果是空载的状态
if (agvs[i].getCurrentX() == agvs[i].getStartX() && agvs[i].getCurrentY() == agvs[i].getStartY()) { // 如果到达起点
agvs[i].setLoad(true); // 设置为负载状态
} else {
// 否则行驶到起点
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 = astar.getPath(start_node, end_node);
path.erase(path.begin());
paths[i] = path;
}
}
```
阅读全文