0

I'm new to Qt, so I might mangle this question. Having said that-

I'm rendering an image within a subclassed QGraphicsView. I added the image to the scene as a Pixmap with addPixmap(). I'd like to overlay (blit) smaller images on top of the larger one in specific locations. I can add the smaller image to the scene as well by again calling addPixmap(), but it always displays in the upper left corner. I'd like to set those coordinates myself.

How can I accomplish this?

Thanks!

Mike O'Malley
  • 335
  • 1
  • 4
  • 17

1 Answers1

1

QGraphicsScene::addPixmap returns a pointer to the added QGraphicsPixmapItem. If you want to set its position, you can do something like this:

QGraphicsPixmapItem *item = scene->addPixmap(yourPixmap);
item->setPos(50, 50); // sets position to scene coordinate (50, 50)

If you want to overlay images on top of other images, make sure you know about z-values. See the QGraphicsItem documentation for details. Basically, the z-value determines the stacking order.

Lastly, familiarize yourself with parenting of QGraphicsItems. When a QGraphicsItem has a parent item, it means (among other things) that its coordinates are expressed in terms of its parents' coordinates. So if an item has a position of (0, 0), but it's the child of an item whose scene position is (50, 50), then the child item will be displayed at (50, 50). So, given the above example, you could then do:

QGraphicsPixmapItem *childItem = new QGraphicsPixmapItem(item);

This creates a new item, "childItem", whose parent is "item". Its coordinates are (0, 0), because they haven't been set yet, but its scene coordinates are (50, 50), because it is the child of "item". Note that when you specify an item's parent, you don't need to add the child item to the scene; it is implicitly added.

Anthony
  • 8,570
  • 3
  • 38
  • 46
  • So if the large image is offset within the scene (which it is, as it's a rectangular image inside a square box), which frame of reference is the mouse event set within? If QMouseEvent() gives me coordinates of (50,50) from within the subclassed QGraphicsView, those coordinates are relative to the graphics view, correct? Is there an easy (automated) way to translate those coordinates to match the pixels within the larger image? – Mike O'Malley Apr 24 '12 at 19:17
  • Yes, those coordinates are in view coordinates. QGraphicsView has an itemAt() method that will return a pointer to whichever item is at the given coordinates. It could be the parent item, could be the child... you can try QGraphicsItem::parentItem to see whether or not an item has a parent. Yes, there is an easy way to translate coordinates: see the mapping functions of QGraphicsItem, QGraphicsScene, and QGraphicsView. Also take a look at [The Graphics View Coordinate System](http://qt-project.org/doc/qt-4.8/graphicsview.html) for a nice explanation. – Anthony Apr 24 '12 at 19:31
  • Also note that--at least in my experience--it's usually much more useful to subclass QGraphicsItem (QGraphicsPixmapItem, in your case), or QGraphicsScene, than to subclass QGraphicsView. These all have mouse handlers of their own. – Anthony Apr 24 '12 at 19:42