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.
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