4

I have a custom implementation of qGraphicsScene and a custom qGraphicsItem that I click on, but the itemAt function never returns a value, even though I am fairly certain that I'm clicking on the item.

void VScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if ((vMouseClick) && (event->pos() == vLastPoint)) {
        QGraphicsItem *mod = itemAt(event->pos(), QTransform());
        if (mod) { // Never returns true
            // ...
        }
    }
}

For clarity, the module is added in the following code:

void VScene::addModule(QString modName, QPointF dropPos)
{
    VModule *module = new VModule();
    addItem(module);
    // the QPointF value comes from an event in mainWindow, the coordinate is mapped to my scene.
    module->setPos(dropPos);
}

... and here is the custom qGraphicsItem that I have written.

VModule.h:

class VModule : public QObject, public QGraphicsItem
{
    public:
        explicit VModule();
        QRectF boundingRect() const;
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

    private:
        qreal w;
        qreal h;
        int xAddr;
        int yAddr;
        QPolygonF baseShape;
}

VModule.cpp:

VModule::VModule()
{
    w = 80;
    h = 80;
    xAddr = w / 2;
    yAddr = h / 2;

    // Use the numbers to create a number of polygons
    QVector<QPointF> basePoints = { QPointF(0.0, 0.0),
                                    QPointF(xAddr, yAddr),
                                    QPointF(0.0, yAddr * 2),
                                    QPointF(-xAddr, yAddr) };
    baseShape = QPolygonF(basePoints);
}

QRectF VModule::boundingRect() const
{
    return QRectF(-xAddr, 0, w, h);
}

void VModule::paint(QPainter *painter, const QStypeOptionGraphicsItem *option, QWidget *widget)
{
    // brushes and so on are set
    // ...

    painter->drawPolygon(baseShape, qt::OddEvenFill);

    // there are other polygons are drawn in the same way as above
}

Are there any problems with my implementation? Is there something I am missing? Thanks in advance for any help.

c0nn
  • 317
  • 3
  • 18
  • May be it some coordinates translation problem (for example, you get absolute screen coordinates, but scene expects its own coordinates), while I'm not sure – Lol4t0 Jun 04 '13 at 14:25
  • @Lol4t0 The event is created and handled in the scene, so I don't think absolute coordinates are the problem. AFAIK QGraphicsItem's also use scene coordinates? – c0nn Jun 04 '13 at 14:57

1 Answers1

3

You are querying the scene in item coordinates instead of scene coordinates. Use:

...
QGraphicsItem *mod = itemAt(event->scenePos());
...
cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • Ha, should have noticed that, thanks. The compiler won't accept omitting the transformation argument, any idea why? – c0nn Jun 04 '13 at 15:02
  • 2
    The transform should come from the view. So you should test the `QGraphicsSceneMouseEvent::widget()` member if it is a `QGraphicsView` and if it is, extract the `transform()` from it. – cmannett85 Jun 04 '13 at 15:13