[Interest] Aligning objects in QGraphics....

BRM bm_witness at yahoo.com
Wed Aug 8 23:25:33 CEST 2012

I have three custom QGraphicsWidgets. Two of them belong to a custom layout that is assigned to the third:

  - custom layout
       - widget2
       - widget3

I am trying to move widget2 to overlay widget3 at specific coordinates; however, the results are a bit confusing.

Widget3 has a signal to notify widget2 of the coordinates it is to align two. Both sets are converted to Scene Coordinates before being emitted via the signal:

Q_EMIT coordinateNotifier(mapToScene(coordinate);

This is then received by the widget2 which converts it to local coordinates:

storedCoordinate = mapFromScene(remoteCoordinate);

And then calls a reposition which converts it to Parent coordinates:

QPointF origin = mapToParent(storedCoordinate);

It then sets its position to this new coordinate:


However, everytime it sets its position, the overlay widget moves increasingly farther away from where it should be. Kind of random in where it goes, but kind of not in that is does mostly the same thing every time.

From the 4.7 documentation, setPos() is suppose to be in parent coordinates, or if no parent then Scene coordinates. Widget2 very explicitly does have a parent - setParentItem() is set to widget1; but mapToParent() should generate appropriate coordinates, no?

In order to align them, I have to first be able to put them properly over top of each other to a known position consistently. After that, I've still got to move it relative to the known coordinate. (The parent widget and/or its layout will be responsible for any scaling, etc is uniform between them.)  I do a couple small tranforms during paint() (during which I save/reset tranform/restore), but that shouldn't affect the position, should it? (The coordinate I'm aligning against is marked by widget3 before any transforms are done, and I can see that it aligns its data to that point properly. Now I'm trying to align the other widget over that point as well.)

The custom layout simply computes a layout geometry that would contain both widgets and account for any overlapping.

Based on my understanding of the documentation, then the above should work. Or am I missing something? (I can do this movement in widget1, but it would mean exposing more APIs than if I did in inside widget2 itself.)

What am I missing?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120808/45918720/attachment.html>

More information about the Interest mailing list