Untransformable QGraphicsItem tags / labels

Last summer, while we were finishing up 4.2, there was a great deal of activity on the mailing lists we set up for the 4.2 technical preview (http://lists.trolltech.com/). Some people kept on asking for support for "untransformable" items, that is, special QGraphicsItem items that don't follow the parent's or view's transformations. Like the text labels in Google Earth, they stay horizontal when you rotate, and don't change their size when you zoom.

Because of the risk of destabilizing the base behavior, we decided to postpone such a feature, and it was moved into research.

Good news, it looks like we will be adding this feature to 4.3. It's still in research, but working quite well. There are some minor details to iron out, but mostly under the hood and they will not affect the API or behavior in any way.

Click to see a short animation.

To trigger this special behavior, you set a flag: ItemIgnoresViewTransformations (, or something like that). Then you can add the item as a regular child of another item. Its position will then show where the item is "anchored" to its parent. And the item is rendered without any rotation or scaling imposed by the view. For example:

QLinearGradient gradient(-100, -100, 0, 100);
gradient.setColorAt(0, QColor(128, 255, 192));
gradient.setColorAt(1, QColor(63, 255, 63));

// Create a scene, add the main ellipse.
QGraphicsScene scene;
QGraphicsEllipseItem *ellipse
= scene.addEllipse(QRectF(-100, -100, 200, 200),
QPen(Qt::black, 2), gradient);

// Add two child lines that cross over the ellipse.
QGraphicsLineItem *line1 = new QGraphicsLineItem(-100, 0, 100, 0, ellipse);
line1->setPen(QPen(Qt::black, 3));
QGraphicsLineItem *line2 = new QGraphicsLineItem(0, -100, 0, 100, ellipse);
line2->setPen(QPen(Qt::black, 3));

// Add five _untransformable_ text items around the ellipse.
for (int i = 0; i setParentItem(ellipse);
text->setPos(100 * cos((6.28 * i) / 5.0), 100 * sin((6.28 * i) / 5.0));

QFont font;

The items still follow regular items' rendering constraints (e.g., the stacking order is the same as with normal items). You can also rotate or scale the item itself; that still works. It just doesn't inherit any transformations.

PS: The Hell Wyrm has been defeated! It took me a good 1.5 hrs of continuous struggle to nail this mark. Can you believe that it's got 6 million HP?? ;-)

Blog Topics: