Shifting a QGraphicsItem's "graphic" - qt

I have a QGraphicsItem with a custom graphic as shown below in the top half of the figure, where the red circle is the shape() of the item:
Is there a way to preserve the graphic but just shift it like shown in the bottom half of the figure?

I don't think, you can move QGraphicsItem without shape. Shape is bounded to QGraphicsItem.
It changes its co-ordinates according to QGraphicsItem.
If you want to achieve something like your second half of the fig. You need to have two QGraphicsItems (one is your red outer circle and second your inner graphics item).

Related

How to remove Ghost Lines drawn in qgraphicsview

I am trying to make a simple program in which I have added a qgraphics scene and in this I have added a QGraphicsRectItem. I have implemented mouse press event, paint event, bounding rect. Now I have drawn a point on one side of rectangle because there can be multiple rectangle I can drop on screen so just to differentiate between them of different color. Now I can move my rectangle inside graphics seen and can increase the size of rectangle by moving it's one side at a time. The problem that I am facing is when I trying to draw point on one side of rectangle at the time of moving it, it leaves traces on graphics scene. can I remove the ghost lines?
This happens either because your boundingRect method isn't correct, or because you forgot to call prepareGeometryChange before making changes that affect the boundingRect result. Your boundingRect needs to include space for line widths, for example; that's a common mistake.

How to get absolute coordinates with QGraphicsScene?

Let's say I have five objects, with the top-left red square and the larger black square both inserted into the scene at 0,0.
.
Later, let's say I want to move the bottom right corner of the black square to the bottom right corner of the bottom-right square. The most obvious thing to do would be:
bS.setRect( bS.rect().setBottomRight(redBR.rect().bottomRight) );
That's not exactly correct code, but the idea is simple. I would set the coordinate of the black square's bottom right corner to that of the red square's bottom right corner. Because that's where I want it to be. But in Qt, this seems to be impossible.
Because the call to redBR thinks it's at the origin. Every item thinks it's at the origin. Which means I can never know the coordinates of any item, anywhere, ever.
Is there a way to force QGraphicsScene to tell me the actual coordinates? Alternatively, is there some other graphics framework that uses real coordinates? I'm not going to insist on using Qt if there is no way to make it work.
Any help would be welcome. Please bear in mind my goal is not: "drag corner of box by manipulator". My goal is to be able to put items at coordinates whenever I want an item to be at a specific coordinate.
Edit:
Here's an example of what I mean. The big box hasn't been connected yet, so don't worry about it's coordinates. The problem is that if I don't remap the points coming out of the little box, every box believes it is at the origin. But if I do, then the y values vacillate between 0, 1, and -1.
The base class QGraphicsItem has quite some useful functionality for mapping between scene and item coordinates. One useful function is for mapping a point in item coordinates to scene coordinates:
QPointF QGraphicsItem::mapToScene(const QPointF &point)
There is however also a direct mapping available between points in different QGraphicsItems, which is probably the easiest to use here:
QPointF QGraphicsItem::mapFromItem(const QGraphicsItem *item, const QPointF &point)
which can be used to map a point in redBR to a point in bS like:
bS.rect().setBottomRight(bS.mapFromItem( &redBR, redBR.rect().bottomRight() ) )

Painting on the margins of QScrollArea

I want to draw something like x\y axes scales like rules in Photoshop but with QScrollArea
There is good property for that:
QAbstractScrollArea::setViewportMargins(20,20,0,0)
and it works fine - there white space at the top and left sides and putting point to (0,0) draw it with specified offset.
But I cant draw at this place.
I tried
painter.drawLine(-10,0,-10,height())
but it do nothing. So how can I draw something on this margin space?
You're painting on the viewport, and this won't work. You need to put a widget in the margin area and paint within that widget's paintEvent.

Converting mouse coordinates to label coordinates in Qt

I'm in trouble with such issue:
I need to select some area with rect on my label, I'm using QRubberBand to do this, but there is one problem: I need to know coordinates of current rect on my label, so I'm in trouble with it, because mouseEvent->pos() give coordinates which starts with top left corner on mainWindow border, moreover i've rotate standard coords on label (from top left to bottom left corner, as we paint them usually).
Anybody knows how can I do this translation?
QPoint mappedPos = myLabel->mapFromParent(myWindow, mouseEvent->pos());
Also, QTransform provides a number of map() functions which should be able to get you the point in rotated coordinates as well.
See:
QWidget::mapFromParent()
QTransform::map()

Handling inverted pixels in bitmapData and bitmap class in as3

I am using bitmapData and bitmap classes to render a mouse cursor on the display screen. The bitmapData consists of an area whose colors should be inverted according to the background color. This is a very basic thing which could be observed with text cursor(the vertical line with two small horizontals on top and bottom), when moved over the text area.
I want to be able to do the same with the pixels in my bitmapData, is there a way to find out the background color effectively and invert the color values?
In this process i will be redrawing the whole pixels, is there any other efficient way to do that ?
You can draw your cursor using BlendMode.INVERT
http://livedocs.adobe.com/flex/3/langref/flash/display/BitmapData.html#draw()
or simply put your cursor display object over your bitmap and set it's blendMode to INVERT.

Resources