[Qt-interest] ItemIgnoresTransformations, z-values and mouseclicks

Esben Mose Hansen kde at mosehansen.dk
Thu Oct 22 12:00:38 CEST 2009


Hi,

I wanted to have some icons on top of my scene (using QGraphicsSvgItem) to 
hightlight problematic areas to the user. To keep aspect ratio I tried to use 
the ItemIgnoresTransformations flag on the icons, but unfortunately that made 
mouse clicks go right through the icons to the objects below. Furthermore, the 
icon didn't get repainted correctly, meaning that when objects below were 
redrawed they were painted on top, together with a bit of background.

Is this how it is supposed to work? To quote from the documentation:

"You must call deviceTransform() to map coordinates and detect collisions in 
the view."

The icon itself is extremely simple: Simply a QGraphicsSvgItem-subclass with 
the following constructor:

     12 problem_item_t::problem_item_t(const problem_t* problem, 
QGraphicsItem* parentItem):
     13     QGraphicsSvgItem(QString::fromUtf8(":/svg/svg/warning.svg"), 
parentItem),
     14     m_problem(problem) {
     15   Q_ASSERT(parentItem); // Should listen on parentItem changes
     16   parent_item_changed();
     17   setZValue(10.0);
     18   setFlag(QGraphicsItem::ItemIgnoresTransformations);

where parent_item_changed() is a badly-named function with this content:

     22 void problem_item_t::parent_item_changed() {
     23   QGraphicsItem* parent = parentItem();
     24   QTransform transformation = sceneTransform();
     25   QSize svg_size = renderer()->defaultSize();
     26   QRectF parent_bound(transformation.mapRect(parent->boundingRect()));
     27   // Set size relatively to the parents item size.
     28   qreal size = std::min(parent_bound.height(), 
parent_bound.width())/2.0;
     29   setTransform(QTransform(size, 0.0, 0.0, size, 0.0, 0.0));
     30   setPos(0.0*svg_size.width(), 0.98*svg_size.height());
     31 }

If I changed 0.98 to 1, the item wasn't drawn at all, which I found strange, 
too.

For now, I have worked around it by not setting the ItemIgnoresTransformations 
flag, and setting the icon's transformation to the inverse scene 
transformation, thus undoing it. It works, but is a bit ugly.

-- 
kind regards, Esben



More information about the Qt-interest-old mailing list