How to change coordinate system on Pixmap - qt

Who knows how to move from top left coordinate system to the default, where X/Y-axis starts from the left bottom corner (like we always draw them) on QPixmap/Qimage

Here's the solution:
QMatrix m;
m.translate(0,size.height());
m.scale(1,-1);
QPainter painter;
painter.begin(&pic);
painter.setMatrix(m);

Related

Calculating resizing QTransform of a rotated rectangle

I'm trying to implement some behavior in my Qt application, where the use can resize and rotate elements that appear on the screen. I'm trying to work out how to rotate and resize a rectangle using QTransform.
First, I implemented rotation for a rectangle (with the origin at its center) as follows:
// Step 1
QRectF rect (100,100, 200, 300);
QTransform rotation;
rotation.translate(rect.center.x(), rect.center.y());
rotation.rotate(45);
rotation.translate(-rect.center.x(), -rect.center.y());
// get rotated polygon
auto polygon = rotation.mapPolygon(rect);
// draw using QPainter
painter->drawPolygon(polygon);
Now, what I would like to do is to resize the rotated rectangle along one of its edges by changing its width, but I'm not quite sure how to go about this. I would like to compute the QTransform matrix that would resize the rectangle (the only thing I worked out is that the transformation origin of the new transform needs to be set to the middle right edge of the rotated rectangle).
If you want to scale along some line that is not horizontal and not vertical, but angled to axis, you need to rotate transformation first in opposite direction by that angle, scale by axis and rotate back.
In your case you can just scale before rotating.

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() ) )

How do I change the coordinate system in Compose.jl?

I am using Compose.jl, and by default it seems that (0,0) corresponds to the top left corner of the image. Is there any way to change the coordinate system within a piece of code such that (0,0) is instead the bottom left corner of the image, and increasing the y value moves up, not down?
To set up the coordinate system use the UnitBox, by default the origin is top-left (0,0). To move the origin to bottom-right and fix the height and width to say |1| try this, UnitBox(1,1,-1,-1).

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()

qt, draw center of pixmap at a point, instead of top-left-corner of pixmap at a point

Like the title says, I'm drawing a pixmap onto a scene at a specific location. QT draws the pixmap by placing the top left corner of the pixmap at the point I specify. However, I want the center of the pixmap to be placed at this point instead. (Pixmap is crosshairs). Does anyone know how to do this? Thanks in advance
void QPainter::translate ( const QPointF & offset )
Translates the coordinate system by the given offset; i.e. the given offset is added to points.
painter.translate(specific_location);
painter.drawPixmap(-pixmap.rect().center(), pixmap);
Translate the specified point by half of the pixmap's width to the left and half of the pixmap's height to the top, then draw pixmap to that point. Just some maths. :)

Resources