//畫出路綫 if (i < paths.size() && paths[i].size() > 1 && paths[i][0] != nullptr) { QPainterPath path; path.moveTo(paths[i][0]->x, paths[i][0]->y); for (int k = 1; k < paths[i].size(); k++) { if (paths[i][k] != nullptr) { path.lineTo(paths[i][k]->x, paths[i][k]->y); } } if (!path.isEmpty()) { QPainter painter(this); if (painter.isActive()) { painter.setPen(QPen(Qt::red, 2)); painter.drawPath(path); } } },修改代碼:循環所有的agv,畫出path的路徑
时间: 2023-12-21 12:03:18 浏览: 71
可以尝试以下修改代码:
```
for (int i = 0; i < agvs.size(); i++) {
//畫出路綫
if (i < paths.size() && paths[i].size() > 1 && paths[i][0] != nullptr) {
QPainterPath path;
path.moveTo(paths[i][0]->x, paths[i][0]->y);
for (int k = 1; k < paths[i].size(); k++) {
if (paths[i][k] != nullptr) {
path.lineTo(paths[i][k]->x, paths[i][k]->y);
}
}
if (!path.isEmpty()) {
QPainter painter(this);
if (painter.isActive()) {
painter.setPen(QPen(Qt::red, 2));
painter.drawPath(path);
}
}
}
}
```
这样就会循环所有的 agv,画出每个 agv 对应的路径。
相关问题
for (int i = 0; i < agvs.size(); i++) { //畫出路綫 if (i < paths.size() && paths[i].size() > 1 && paths[i][0] != nullptr) { QPainterPath path; path.moveTo(paths[i][0]->x, paths[i][0]->y); for (int k = 1; k < paths[i].size(); k++) { if (paths[i][k] != nullptr) { path.lineTo(paths[i][k]->x, paths[i][k]->y); } } if (!path.isEmpty()) { QPainter painter(this); if (painter.isActive()) { painter.setPen(QPen(Qt::red, 20)); painter.drawPath(path); } } } },修改爲:畫出agv的整個路徑
可以将上述代码修改为:
```
for (int i = 0; i < agvs.size(); i++) {
if (i < paths.size() && paths[i].size() > 1 && paths[i][0] != nullptr) {
QPainterPath path;
path.moveTo(paths[i][0]->x, paths[i][0]->y);
for (int k = 1; k < paths[i].size(); k++) {
if (paths[i][k] != nullptr) {
path.lineTo(paths[i][k]->x, paths[i][k]->y);
}
}
if (!path.isEmpty()) {
agvs[i]->setPath(path); // 设置AGV的整个路径
QPainter painter(this);
if (painter.isActive()) {
painter.setPen(QPen(Qt::red, 20));
painter.drawPath(path);
}
}
}
}
```
这样就可以同时画出AGV的整个路径,并将路径保存在AGV对象中。注意需要在AGV类中添加`setPath`方法来保存路径。
std::vector<std::vector<Node*>> paths(agvs.size()); for (int i = 0; i < agvs.size(); i++) { //畫出agv獲取path上的坐標點之間的連綫 if (i < paths.size() && paths[i].size() > 1 && paths[i][0] != nullptr) { QPainterPath path; path.moveTo(paths[i][0]->x, paths[i][0]->y); for (int k = 1; k < paths[i].size(); k++) { if (paths[i][k] != nullptr) { path.lineTo(paths[i][k]->x, paths[i][k]->y); if (painter.isActive()) { painter.setPen(QPen(Qt::red, 20)); painter.drawLine(paths[i][k-1]->x, paths[i][k-1]->y, paths[i][k]->x, paths[i][k]->y); } } } } },爲什麽沒有繪製出直綫
根据提供的代码,似乎是缺少 `painter.strokePath(path, QPen(Qt::red, 20));` 这一行代码,导致没有绘制出直线。可以尝试将以下代码:
```
if (painter.isActive()) {
painter.setPen(QPen(Qt::red, 20));
painter.drawLine(paths[i][k-1]->x, paths[i][k-1]->y, paths[i][k]->x, paths[i][k]->y);
}
```
修改为:
```
if (painter.isActive()) {
painter.setPen(QPen(Qt::red, 20));
painter.drawLine(paths[i][k-1]->x, paths[i][k-1]->y, paths[i][k]->x, paths[i][k]->y);
painter.strokePath(path, QPen(Qt::red, 20));
}
```
这样可以在绘制直线的同时,使用 `painter.strokePath()` 方法将路径绘制出来。
阅读全文