Qt GUI开发 - 使用QGraphicsView显示2D网格 [英] Qt GUI Development - Displaying a 2D grid using QGraphicsView

查看:3453
本文介绍了Qt GUI开发 - 使用QGraphicsView显示2D网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt开发的新手,所以我试图研究一个解决方案,我需要设计的用户界面。我的项目是模拟一个在线游戏中的球员在全球地图上移动。为了表示地图,我需要显示一个2D网格,网格中的每个空间表示地图的一个区域。然后我需要在游戏中显示每个玩家的位置。后端是完全工作,地图实现为二维数组。我只是停留在如何显示网格。

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.

我做的研究让我相信一个QGraphicsView是最好的方法,但我可以似乎找到一个与我需要的相关的教程。

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.

感谢,Dan

推荐答案

2D网格只是一组水平和垂直线。假设您有一张500x500的地图,并且要绘制一个网格,其中两个方向的线之间的距离为50.下面的示例代码显示了如何实现它。

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.

// 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 开发人员日。

You should check the QGraphicsView and the QGraphicsScene documentation as well as the corresponding examples. Also you can watch the graphics view training videos or some graphics view related videos from the Qt developer days.

这篇关于Qt GUI开发 - 使用QGraphicsView显示2D网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆