what is the qtransform in QGraphicsScene::itemAt()
Asked Answered
L

1

12

I create a custom QGraphicsItem. And overwrite the boundingRect() and paint().

QRectF myTile::boundingRect() const
{
  return QRectF(xPos*10, yPos*10, 10, 10);
}

void myTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
  QRectF rec = boundingRect();
  int gvi = value * 255;
  QColor gv(gvi, gvi, gvi, 255);
  QBrush brush(gv);
  painter->fillRect(rec, brush);
  painter->drawRect(rec);
}

Then I use addItem() to add a item to a scene. Now I want to get it from the scene by its position. I find the itemAt function. But the problem is I don't know what is the const QTransform & deviceTransform. What should I use for the QTransform?.

Because I didn't implement any transform in the QGraphicsItem. This confuses me.

Livelong answered 24/12, 2012 at 20:49 Comment(0)
F
5
QGraphicsItem * QGraphicsScene::itemAt ( const QPointF & position, const QTransform & deviceTransform ) const

Returns the topmost visible item at the specified position, or 0 if there are no items at this position. deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations. This function was introduced in Qt 4.6.

So I would say, if you have the need to transform some items and ignore the others, you can simply go with the default value of QTransform() or even better the QGraphicsView::transform() const.

soo long zai

Fausta answered 25/12, 2012 at 13:55 Comment(1)
Mind though that a QGraphicsScene can have more than one view. So the value of deviceTransform depends on the context. For example, in a mouse event handler you'd have to figure out from which view the event came from. See this comment.Tomlinson

© 2022 - 2024 — McMap. All rights reserved.