C++ Qt GUI 开发 - 使用 QGraphicsView 显示 2D 网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8279567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt GUI Development - Displaying a 2D grid using QGraphicsView
提问by dlwells02
I'm new to Qt development so I've being trying to research a solution to a user interface I need to design. My project is to simulate players in an online game moving around a global map. To represent the map I need to display a 2D grid, with each space in the grid representing a region of a map. I then need to display the location of each player in the game. The back-end is all fully working, with the map implemented as a 2D array. I'm just stuck on how to display the grid.
我是 Qt 开发的新手,所以我一直在尝试研究我需要设计的用户界面的解决方案。我的项目是模拟在线游戏中的玩家在全球地图上移动。为了表示地图,我需要显示一个 2D 网格,网格中的每个空间代表地图的一个区域。然后我需要显示每个玩家在游戏中的位置。后端全部正常工作,地图实现为二维数组。我只是被困在如何显示网格上。
The research I have done has led me to believe a QGraphicsView is the best way to do this, but I can't seem to find a tutorial relevant to what I need. If anyone has any tips on how to implement this it would be much appreciated.
我所做的研究使我相信 QGraphicsView 是做到这一点的最佳方式,但我似乎找不到与我需要的相关的教程。如果有人对如何实现这一点有任何提示,将不胜感激。
Thanks, Dan
谢谢,丹
回答by pnezis
A 2D Grid is nothing more than a set of horizontal and vertical lines. Suppose you have a 500x500 map and you want to draw a grid where the distance between the lines in both directions is 50. The sample code that follows shows you how you can achieve it.
二维网格只不过是一组水平和垂直线。假设您有一张 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());
You should check the QGraphicsView
and the QGraphicsScene
documentation as well as the corresponding examples. Also you can watch the graphics view training videosor some graphics view related videosfrom the Qt developer days.
您应该检查QGraphicsView
和QGraphicsScene
文档以及相应的示例。您还可以观看Qt 开发者时代的图形视图培训视频或一些图形视图相关视频。
回答by fady mohamed osman
Well if you have a constant grid size or even a limited number of grid sizes what i like to do is to draw a grid block in gimp or any other program and then set that as the background brush (draw only bottom and right side of the block) qt will repeat the image and will give you a full grid. I think this is good for performance too.
好吧,如果您有一个恒定的网格大小甚至有限数量的网格大小,我喜欢做的是在 gimp 或任何其他程序中绘制一个网格块,然后将其设置为背景画笔(仅绘制底部和右侧的块)qt 将重复图像,并会给你一个完整的网格。我认为这对性能也有好处。
This is the grid image i used in one of my programs it's 10x10 pixels.
这是我在我的一个程序中使用的网格图像,它是 10x10 像素。
Then call QGraphicsScene
setBackgroundBrush as the follwing:
然后调用QGraphicsScene
setBackgroundBrush 作为以下内容:
scene->setBackgroundBrush(QBrush(QPixmap(":/grid/grid10.png")));
回答by cambalamas
The more native way is this:
更原生的方式是这样的:
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.
Sorry by PyQt...
抱歉,PyQt...
回答by Sumit
Suppose a scene is set to the graphicsview then simply below one line will show the grid.
假设一个场景设置为图形视图,那么在一行下方将显示网格。
ui->graphicsView->scene()->setBackgroundBrush(Qt::CrossPattern);
There several other values can be passed for ex: Qt::Dense7Pattern
可以为 ex 传递其他几个值: Qt::Dense7Pattern
These are members of enum BrushStyle
, just click on any used value in Qt creator and it will take you to the enum declaration where you can see all other possible values.
这些是 的成员enum BrushStyle
,只需单击 Qt creator 中任何使用的值,它就会带您到枚举声明,在那里您可以看到所有其他可能的值。
PS: A scene can be set like this:
PS:一个场景可以这样设置:
ui->graphicsView->setScene(new QGraphicsScene());