1

I have QGraphicsLayoutItem placed inside a QGraphicsLinearLayout. This is added to my QGraphicsWidget.

void MyCustomQGraphicsWidget::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
    if (mouseEvent->button() == Qt::LeftButton) {
        qDebug() << "clicked inventory";
    }
}

I am having trouble finding a way to get e.g. the index of the item clicked in the layout.

ovg
  • 1,486
  • 1
  • 18
  • 30

1 Answers1

0

There is no direct way to do it. But you could calculate and index and use the itemAt() method, suppose the mouse y coordinate is mouseY and item height is H. You can write:

int index = mouseY / H; // could be minus some margin
QGraphicsLayoutItem *item = layout.itemAt(index);
Mahmoud Badri
  • 1,256
  • 14
  • 24