nicodega
14th April 2009, 15:49
Hi, I'de like to tell you about something I found out yesterday with Qt 4.5 (on windows), and as I'm not a graphics guru, I'd like to hear what you think about it.. if I should report it as a bug to Qt, or if you think it's not a bug.
here it goes:
If you create a QGraphicsItem, rotate it and then you use setPos, if you try to map the bounding rectangle to it's parent, it won't be translated correctly.
Example: (the dots are missing code, but you can get the idea)
class MyQGraphicsItem : public QGraphicsPixmapItem
{
public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QGraphicsPixmapItem::paint(painter, option, widget);
// this item has not paintable region
if(childItems().length() != 0)
{
painter->drawRect(childItems().at(0)->mapRectToParent(childItems().at(0)->boundingRect()));
}
}
};
MyQGraphicsItem *parentItem = new QGraphicsPixmapItem(...);
QGraphicsPixmapItem *childItem = new QGraphicsPixmapItem(parentItem,.....);
childItem->rotate(180);
childItem->setPos(0,200);
parentItem->setPos(100,100);
On the example, the drawn rectangle will clearly not be as it is supposed to. (it will have the right size and rotation, but the x and y possitions will be wrong)
Now, if we look at mapRectToParent code, it'll first translate the Rect, and then apply the transformation. If the order is changed (i.e firts transform then move to the item possition) It'll be drawn where it's supposed to.
Defining MyGraphicsItem class paint method this way, fixes the problem, and the rectangle is drawn properly:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QGraphicsPixmapItem::paint(painter, option, widget);
// this item has not paintable region
if(childItems().length() != 0)
{
QRectF r = childItems().at(0)->transform().mapRect(childItems().at(0)->boundingRect());
r = r.translated(childItems().at(0)->x(), childItems().at(0)->y());
painter->drawRect(r);
}
}
I found this, because I noticed when one of my items was rotated (only transform I tried) it would dissapear from screen sometimes when moving it around. (I've implemented a custom drag method but in the end all it does is use setPos).
After hours of trying to understand the inner workings of Qt, I found the child item boundingRect sometimes did not intersect the invalidated region, although it was supposed to, and it led me to check if the mapping was being properly done.
Well... what dou you think? I sure looks like a bug to me hehe, but should I report it to the trolls?
here it goes:
If you create a QGraphicsItem, rotate it and then you use setPos, if you try to map the bounding rectangle to it's parent, it won't be translated correctly.
Example: (the dots are missing code, but you can get the idea)
class MyQGraphicsItem : public QGraphicsPixmapItem
{
public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QGraphicsPixmapItem::paint(painter, option, widget);
// this item has not paintable region
if(childItems().length() != 0)
{
painter->drawRect(childItems().at(0)->mapRectToParent(childItems().at(0)->boundingRect()));
}
}
};
MyQGraphicsItem *parentItem = new QGraphicsPixmapItem(...);
QGraphicsPixmapItem *childItem = new QGraphicsPixmapItem(parentItem,.....);
childItem->rotate(180);
childItem->setPos(0,200);
parentItem->setPos(100,100);
On the example, the drawn rectangle will clearly not be as it is supposed to. (it will have the right size and rotation, but the x and y possitions will be wrong)
Now, if we look at mapRectToParent code, it'll first translate the Rect, and then apply the transformation. If the order is changed (i.e firts transform then move to the item possition) It'll be drawn where it's supposed to.
Defining MyGraphicsItem class paint method this way, fixes the problem, and the rectangle is drawn properly:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QGraphicsPixmapItem::paint(painter, option, widget);
// this item has not paintable region
if(childItems().length() != 0)
{
QRectF r = childItems().at(0)->transform().mapRect(childItems().at(0)->boundingRect());
r = r.translated(childItems().at(0)->x(), childItems().at(0)->y());
painter->drawRect(r);
}
}
I found this, because I noticed when one of my items was rotated (only transform I tried) it would dissapear from screen sometimes when moving it around. (I've implemented a custom drag method but in the end all it does is use setPos).
After hours of trying to understand the inner workings of Qt, I found the child item boundingRect sometimes did not intersect the invalidated region, although it was supposed to, and it led me to check if the mapping was being properly done.
Well... what dou you think? I sure looks like a bug to me hehe, but should I report it to the trolls?