我是Qt开发的新手,所以我试图研究一个我需要设计的用户界面的解决方案.我的项目是模拟一个在全球地图上移动的在线游戏玩家.要表示地图,我需要显示一个2D网格,网格中的每个空格表示地图的一个区域.然后我需要显示每个玩家在游戏中的位置.后端是完全正常的,地图实现为2D阵列.我只是坚持如何显示网格.
我所做的研究使我相信一个QGraphicsView是最好的方法,但我似乎找不到与我需要的教程.如果任何人有任何有关如何实现这一点的提示,将非常感谢.
谢谢,丹
解决方法
2D网格只不过是一组水平和垂直的线条.假设你有一张500×500的地图,你想绘制一个网格,两条线之间的距离是50.以下示例代码显示了如何实现.
@H_404_10@// create a scene and add it your view
QGraphicsScene* scene = new QGraphicsScene;
ui->view->setScene(scene);
// Add the vertical lines first,paint them red
for (int x=0; x<=500; x+=50)
scene->addLine(x,x,500,QPen(Qt::red));
// Now add the horizontal lines,paint them green
for (int y=0; y<=500; y+=50)
scene->addLine(0,y,QPen(Qt::green));
// Fit the view in the scene's bounding rect
ui->view->fitInView(scene->itemsVBoundingRect());
您应该检查QGraphicsView
和QGraphicsScene
文档以及相应的examples.此外,您可以从Qt开发人员日期观看图形视图training videos或某些图形视图related videos.