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.
QGraphicsScene is part of the Graphics View Framework. QGraphicsScene also provides functionality that lets you efficiently determine both the location of items, and for determining what items are visible within an arbitrary area on the scene.
These are is viewed through a QGraphicsView. Adding an item is simple, you can just call the QGraphicsScene function addItem, which takes a QGraphicsItem*. In this case, you have a pointer to the item, so if you call QGraphicsScene::removeItem, it will remove it from the scene and you can then delete the item.
QGraphicsView visualizes the contents of a QGraphicsScene in a scrollable viewport. To create a scene with geometrical items, see QGraphicsScene's documentation. QGraphicsView is part of the Graphics View Framework.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With