QGraphicsItem boundingRect coordinates
-
Hi there,
I am implementing a QGraphicsItem and I'm having trouble understanding the coordinate systems. Particularly, I do not understand in what coordinates the boundingRect should be.
MyItem draws a red rectangle across the middle of the screen (see below). I use QPaintDevice's logical coordinates to find the screen width. (This seems weird. Should the item be able to draw in its own coordinate system?). I store the logical coordinate's width in a member variable. This allows me to compute the bounding rect the same way I paint the rectangle. (This also seems weird. The boundingRect method should return a valid result before MyItem::paint gets executed. )
If the bounding rect were correct I would be able to trigger mouse hover events of MyItem. I cannot do that, however.
I'd appreciate any help or clarification on this. Thanks.
The paint method draws a red rectangle.
@
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// Device defines the coordinate system
QPaintDevice *device = painter->device();
m_width = (float) device->width();// Draw stuff painter->setBrush(Qt::red); painter->drawRect(-m_width/2, 0, m_width, 10);
}
@Here is my bounding rect
@
QRectF MyItem::boundingRect() const {
return QRectF(-m_width/2, 0, m_width, 10);
}
@And here is how I setup the scene inside my mainwindow.
@
QGraphicsScene *scene = new QGraphicsScene();
MyItem *item = new MyItem();
scene->addItem(item);
ui->graphicsView->setScene(scene);
@