gpt4 book ai didi

c++ - Qt GUI 开发 - 使用 QGraphicsView 显示 2D 网格

转载 作者:IT老高 更新时间:2023-10-28 23:17:08 39 4
gpt4 key购买 nike

我是 Qt 开发的新手,所以我一直在尝试研究我需要设计的用户界面的解决方案。我的项目是模拟在线游戏中的玩家在全局 map 上移动。为了表示 map ,我需要显示一个 2D 网格,网格中的每个空间都代表 map 的一个区域。然后我需要显示游戏中每个玩家的位置。后端完全正常工作, map 实现为 2D 数组。我只是纠结于如何显示网格。

我所做的研究使我相信 QGraphicsView 是做到这一点的最佳方式,但我似乎找不到与我需要的相关的教程。如果有人对如何实现这一点有任何提示,将不胜感激。

谢谢,丹

最佳答案

2D 网格只不过是一组水平和垂直线。假设您有一个 500x500 的 map ,并且您想绘制一个网格,其中两个方向的线之间的距离为 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());

您应该检查 QGraphicsViewQGraphicsScene文档以及相应的examples .您也可以观看图形 View training videos或一些图形 View related videos从 Qt 开发者时代开始。

关于c++ - Qt GUI 开发 - 使用 QGraphicsView 显示 2D 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8279567/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com