qt - 在 QGraphicsScene 中如何捕获特定项目

发布于 2024-09-06 08:21:59 字数 373 浏览 6 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-09-13 08:21:59

当你说你需要得到国王时,你如何在程序中区分白人国王和黑人国王?

如果您需要获得一个 Pawn,您如何知道是哪一个?有人吗?您可以在您的物品中找到第一个吗?

我没有想太多,但也许你可以做的是使用 QMap。键是不同部分的枚举,值是指向相关 QGraphicsItem 的指针。像这样的事情:

enum Piece_e {
    KING,
    QUEEN,
    ROOK1,
    ROOK2,
    ...
    PAWN1,
    PAWN2,
    ...
};

QMap<Piece_e, QGraphicsPixmapItem*> WhitePiecesItems;
QMap<Piece_e, QGraphicsPixmapItem*> BlackPiecesItems;

当您创建场景并实例化您的作品时,您将填充地图:

...
WhitePiecesItem[KING] = new QGraphicsPixmapItem(QPixmap("whiteking_pic"));
WhitePiecesItem[PAWN1] = new QGraphicsPixmapItem(QPixmap("whitepawn_pic"));
...

BlackPiecesItem[QUEEN] = new QGraphicsPixmapItem(QPixmap("whitequeen_pic"));
BlackPiecesItem[PAWN1] = new QGraphicsPixmapItem(QPixmap("whitepawn_pic"));
...

当您需要找到与白色国王相对应的对象时,您可以执行以下操作:

QGraphicsPixmapItem* pItem = WhitePiecesItem[KING];

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 :

enum Piece_e {
    KING,
    QUEEN,
    ROOK1,
    ROOK2,
    ...
    PAWN1,
    PAWN2,
    ...
};

QMap<Piece_e, QGraphicsPixmapItem*> WhitePiecesItems;
QMap<Piece_e, QGraphicsPixmapItem*> BlackPiecesItems;

When you are creating your scene and instanciating your pieces, you'll fill the map :

...
WhitePiecesItem[KING] = new QGraphicsPixmapItem(QPixmap("whiteking_pic"));
WhitePiecesItem[PAWN1] = new QGraphicsPixmapItem(QPixmap("whitepawn_pic"));
...

BlackPiecesItem[QUEEN] = new QGraphicsPixmapItem(QPixmap("whitequeen_pic"));
BlackPiecesItem[PAWN1] = new QGraphicsPixmapItem(QPixmap("whitepawn_pic"));
...

When you need to find the object corresponding to the white king, you could do something like this :

QGraphicsPixmapItem* pItem = WhitePiecesItem[KING];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文