qt - QGraphicsScene::itemAt() - 如何识别自定义类

标签 qt qgraphicsscene

我有个小问题

我正在编写 Petri Net 模拟器...

我有两个不同的类(class)

    class PNItem : public QObject, public QGraphicsItem
    ...

    class PNEdge : public QGraphicsLineItem

当我打电话时...

    QGraphicsItem *QGraphicsScene::ItemAt(//cursor position)

,有没有可能以某种方式了解我点击了什么项目?分别ItemAt 给了什么元素?

最佳答案

GraphicsItem::type()旨在解决这个问题。

所以你会做这样的事情,例如:

enum ItemType { TypePNItem = QGraphicsItem::UserType + 1,
                TypePNEdge = QGraphicsItem::UserType + 2 }

class PNItem : public QObject, public QGraphicsItem {

    public:
        int type() { return TypePNItem; }
    ...

};

这将允许您这样做:

QGraphicsItem *item = scene->itemAt( x, y );
switch( item->type() )
{
    case PNItem:
         ...
         break;
}

这样做还可以使用 qgraphicsitem_cast

另请参阅:QGraphicsItem::UserType

关于qt - QGraphicsScene::itemAt() - 如何识别自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114812/

相关文章:

qt - 在 QML 中重新设置视觉项的更好方法

c++ - 使 View 的宽度适合 QComboBox 的最长项目

c++ - 如何在 Qt 中将 std::string 转换为 QString?

python - 在 QGraphicsScene 选择更改后为所选项目切换 QPen

python - 如何将图像添加到 QGraphicsScene 以便矩形项目可以位于其上?

c++ - qt-通过线程更新用户界面

c++ - 有什么方法可以在 QT Creator .pro 文件中检测 64 位与 32 位机器吗?

python - 使 QGraphisItem 适合 View

c++ - Qt:使用 QGraphicsscene 显示图像的问题

python - 判断点击事件中是哪个椭圆