qt - 在 QGraphicsScene 中如何捕获特定项目
我的 QGraphicsScene
中有很多东西。情况是我正在创建一个棋盘,并且正在使用图形场景。所以QGraphicsScene
有很多QGraphicsPixmapItems
。现在我怎样才能得到国王。
更新: 在此QGraphicsScene
中,我添加了QGraphicsPixmapItems
,它们只不过是硬币(棋盘、国王、皇后、士兵等)。现在,如果我想移动一枚特定的硬币,比如说 King,那么我怎样才能得到它。有一些方法,例如使用迭代器。但是有没有办法通过名称找到特定的QGraphicsPixmapItem
。
I have so many things in my QGraphicsScene
. The situation is I am creating a chessboard, and is using Graphics scene. So the QGraphicsScene
is having so many QGraphicsPixmapItems
. Now In this how can I get the King.
Update:
In this QGraphicsScene
, I am adding QGraphicsPixmapItems
which are nothing but coins(board,king,queen,soldiers,etc). Now if I want to move a particular coin say King, then How can I get it. There are some methods like using iterators. But is there any way to find a particular QGraphicsPixmapItem
by it's name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你说你需要得到国王时,你如何在程序中区分白人国王和黑人国王?
如果您需要获得一个 Pawn,您如何知道是哪一个?有人吗?您可以在您的物品中找到第一个吗?
我没有想太多,但也许你可以做的是使用 QMap。键是不同部分的枚举,值是指向相关 QGraphicsItem 的指针。像这样的事情:
当您创建场景并实例化您的作品时,您将填充地图:
当您需要找到与白色国王相对应的对象时,您可以执行以下操作:
When you say you need to get the King, how do you make the difference in your program between the white King and the black one ?
If you need to get a Pawn, how do you know which one ? Anyone ? The first one you can find in your items ?
I haven't thought a lot about it, but maybe what you can do is using a QMap. The key would be a enumeration of the different pieces and the value would be a pointer to the relevant
QGraphicsItem
. Something like this :When you are creating your scene and instanciating your pieces, you'll fill the map :
When you need to find the object corresponding to the white king, you could do something like this :