我是Qt开发的新手,所以我一直在努力研究我需要设计的用户界面的解决方案。我的项目是模拟在线游戏中的玩家在全球地图上移动。为了表示地图,我需要显示一个2D网格,网格中的每个空间代表地图的一个区域。然后我需要显示每个玩家在游戏中的位置。后端完全正常工作,map实现为一个2D数组。我只是被困在如何显示网格上。
我所做的研究让我相信QGraphicsView是实现这一点的最好方法,但我似乎找不到与我需要的相关的教程。如果任何人有任何关于如何实现这一点的提示,将不胜感激。
谢谢,丹
发布于 2011-11-27 02:18:23
二维网格只不过是一组水平线和垂直线。假设您有一张500x500的地图,您想要绘制一个网格,其中两个方向上的线之间的距离为50。下面的示例代码向您展示了如何实现它。
// 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,0,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,500,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。
发布于 2013-06-21 21:05:35
好吧,如果你有一个固定的网格大小,甚至是有限的网格大小,我想做的是在gimp或任何其他程序中绘制一个网格块,然后将其设置为背景画笔(只绘制块的底部和右侧),qt将重复该图像,并将给你一个完整的网格。我认为这对性能也有好处。
这是我在我的一个程序中使用的网格图像,它是10x10像素。

然后调用QGraphicsScene setBackgroundBrush,如下所示:
scene->setBackgroundBrush(QBrush(QPixmap(":/grid/grid10.png")));发布于 2017-05-20 07:35:38
更本地化的方式是:
scene = self.getScene() # Your scene.
brush = QBrush()
brush.setColor(QColor('#999'))
brush.setStyle(Qt.CrossPattern) # Grid pattern.
scene.setBackgroundBrush(brush)
borderColor = Qt.black
fillColor = QColor('#DDD')
rect = QRectF(0.0, 0.0, 1280, 720) # Screen res or whatever.
scene.addRect(rect,borderColor,fillColor) # Rectangle for color.
scene.addRect(rect,borderColor,brush) # Rectangle for grid.对不起,由PyQt...
https://stackoverflow.com/questions/8279567
复制相似问题