QPainterPath QTransform::map - c++

I would like to draw a rectangle with an angle. It works but when I change the angle, location of rectangle is changing somewhere else. I couldnt understand it. Does anybody give me a hand?
Here is my code :
QPoint point = QPoint(100,100); // has to be shown at this point
QSize size = QSize(30,30);
QRect rect = QRect(point,size);
QPainterPath Path ;
Path.addRect(rect);
QTransform t;
t.rotate(myAngle);
QPainterPath newPath= t.map(Path);
QwtPlotShapeItem *Item = new QwtPlotShapeItem( "Shape Name" );
Item->setItemAttribute( QwtPlotItem::Legend, true );
Item->setRenderHint( QwtPlotItem::RenderAntialiased, true );
Item->setShape(newPath );
Item->setPen( Qt::black );
Item->setBrush( QColor("Grey") );
Item->attach(this);
I think map() function cause this problem. But i dont know why. Thanks for advices

QTransform::rotate rotates coordinate system using (0, 0) center point. Your rectangle is not at the center, so while rotating it will be significantly moved. You should place your rectangle at the center of coordinate system (point=(-15, -15)) and use t.translate after t.rotate to move rotated rectangle to appropriate position.

Related

How do I rotate photos in QPixmap?

Marked a needle on the speedometer.
// 이미지 출력
QPixmap pix("C:\\Users\\WORK002\\Desktop\\speedmeter.png");
QPixmap pix2("C:\\Users\\WORK002\\Desktop\\pointer.png");
QPixmap pointer = pix2.scaled(300, 300);
scene.addPixmap(pix);
scene.addPixmap(pointer);
ui-> graphicsView ->setScene(&scene);
ui-> graphicsView ->show();
I want rotate and reposition.
How can i do this?
You don't have to mess with the QPixmap, you can manipulate the QGraphicsPixmapItem returned by QGraphicsScene::addPixmap:
QGraphicsPixmapItem *pixmap_item = scene.addPixmap(pix);
To set the position, you can use QGraphicsItem::setPos.
To set the rotation, first set the transform origin point with QGraphicsItem::setTransformOriginPoint,(this will set the point around which your item will be rotated) and then set the rotation with QGraphicsItem::setRotation:
pixmap_item->setPos(50, 0);
pixmap_item->setTransformOriginPoint(pixmap_item->boundingRect().center());
pixmap_item->setRotation(90);
You will have to set the correct values yourself, but this should lead you in the right way.
You can look into QPixmap::transformed()
QPixmap QPixmap::transformed(const QTransform &transform, Qt::TransformationMode mode = Qt::FastTransformation) const
The specification can be given through the QTransform object:
rotate() for rotation
translate() for reposition
may this one can help:
//Please note this method takes 2 mseg to finish for a 20x20 pixmap.
QPixmap rotatedPixmap(m_pixOriginal.size());
rotatedPixmap.fill(QColor::fromRgb(0, 0, 0, 0)); //the new pixmap must be transparent.
QPainter* p = new QPainter(&rotatedPixmap);
QSize size = m_pxOriginal.size();
p->translate(size.height()/2,size.height()/2);
p->rotate(m_iAngle);
p->translate(-size.height()/2,-size.height()/2);
p->drawPixmap(0, 0, m_pxOriginal);
p->end();
delete p;
m_pxItem->setPixmap(rotatedPixmap);
copied from:
Read this forum thread

how to scale graphics properly?

Now I need to draw some polylines according to their coordinates. These are coordinates of one poltline:
1.15109497070313E+02 2.73440704345703E+01
1.15115196228027E+02 2.73563938140869E+01
1.15112876892090E+02 2.73697128295898E+01
1.15108222961426E+02 2.73687496185303E+01
1.15081001281738E+02 2.73908023834229E+01
1.15078292846680E+02 2.73949108123779E+01
1.15073806762695E+02 2.74090080261230E+01
1.15063293457031E+02 2.74221019744873E+01
1.15059646606445E+02 2.74324569702148E+01
I've drawn these polylines and moved them to the center of window:
QPainter painter(this);
QPainterPath path;
for (auto& arc : layer.getArcs()) {
for (int i = 0; i < arc.pts_draw.size() - 1; i++)
{
QPolygonF polygon = QPolygonF(arc.pts_draw);
path.addPolygon(polygon);
}
}
// move all polylines to the center of window
QPointF offset = rect().center() - path.boundingRect().center();
painter.translate(offset);
painter.drawPath(path);
However, what I got in the window was this:
I think it's caused by the coordinates. All coordinates are very close to each other so the graphics will become too small when drawn in the window. So my problem is how to scale the graphics properly? In other words, how can I know the ratio of scaling?
On the QGraphicsView you can call scale(qreal sx, qreal sy) to scale the QGraphicsScene and all it's QGraphicsItems. If you wish to scale each item individually instead of the entire scene, then take each point in the polygon and use Euclidian geometry scaling to scale your polygon. Or you could use something called QTransform like this post did

Qt drawing a ring / circle with a hole

I need to draw a circle with QPainter. When I used drawEllipse function like :
void UserClass::Draw(QPainter &painter) {
painter.save();
painter.setBrush( GetColor() );
QPoint centerPosition = GetCenterPosition();
painter.drawEllipse( centerPosition, m_CircleOuterRadius, m_CircleOuterRadius);
painter.setBrush(QColor(0, 0, 0, 0));
painter.drawEllipse( centerPosition, m_CircleInnerRadius, m_CircleInnerRadius);
painter.restore();
}
Unfortunately result is not what I desired. I want to have inner circle not be filled. That is why I put alpha value as zero but ofcourse it didn't work. How can I have a circle which is not until a certain radius with qt ?
You should create a QPainterPath then add the two circles to it via addEllipse(), the outer first, then the inner. This will effectively give you a shape that is the outer circle with the inner circle punched as a hole.
Then you fill the painter path with a green brush, which will result in a hollow ring. Afterwards, if you want the white outlines, you can stroke the path with a white pen as well.
Also note that the painter path can be created only once and stored for reuse instead of creating it anew every time you redraw.

How to map item coordinates?

I know that every item has own coordinates relative to scene. I am adding an ellipse in the scene. Each of them returns the following from boundingRect(): QRect(0, 0, 50, 50). I don't know how to map coordinates to another QGraphicsItem which is a line. The line supposed to connect this two ellipses. I have correct coordinates of ellipses and I am passing them to a custom QGraphicsLineItem constructor. However the line is in a wrong place. How should I use mapFromItem() or other method to get the result?
I get each ellipse's coordinates as follows:
selfCoords = ellipse->mapFromScene(QPointF(0.0,0.0));
You should map the coordinates from each ellipse to some common coordinate system that you can then map to the line's parent. The common coordinate system can be the scene's coordinate system.
For example, to connect the centers of the ellipses:
QGraphicsScene scene;
QGraphicsEllipseItem e1, e2;
scene.addItem(&e1);
scene.addItem(&e2);
... // set the ellipse rects/sizes
auto start = e1.mapToScene(e1.boundingRect().center());
auto end = e2.mapToScene(e2.boundingRect().center());
QGraphicsLineItem l(QLineF(start, end));
scene.addItem(&l);
You can do this because the line's parent is the scene. Now assume that we had some other parent for the line - you'd need to map the coordinates to that parent instead.
...
QGraphicsItem p;
p(20, 20);
scene.addItem(&p);
auto start = e1.mapToItem(&p, e1.boundingRect().center());
auto end = e2.mapToItem(&p, e2.boundingRect().center());
QGraphicsLineItem l(QLineF(start, end), &p);
If I want to add new ellipse on mouse position, how to map coords to ellipse item to get right position on scene ? For example from contextMenuEvent I get
QPointF coords = event->scenePos(); and there I want to create ellipse. I have custom QGraphicsScene MyScene where I have pointer to QGraphicsView* view.
I use event form void MyScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
QPointF coords = event->scenePos();
QPointF ellpiseCoords = view->mapToScene(coords .x(), coords .y())
I always get wrong transform.

Blit a rectangle at by coordinate

In Pygame and Python 2.7, how can I blit a rectangle at certain point represented by a set of coordinates?
I know I can use this:
screen.blit(img.image, img.rect.topleft)
But I want the rectangle to be at a precise point on the screen.
If you need topleft corner of rectangle in point (34,57) you can do
screen.blit(img.image, (34,57) )
or this
img.rect.topleft = (34,57)
screen.blit(img.image, img.rect)
or this
img.rect.x = 34
img.rect.y = 57
screen.blit(img.image, img.rect)
If you need center of rectange in point (34,57)
img.rect.center = (34,57)
screen.blit(img.image, img.rect)
If you need rectangle in center of the screen:
(especially useful if you need to show text (ex. "PAUSE") in center of the screen, or text in center of rectangle to create button)
img.rect.center = screen.get_rect().center
screen.blit(img.image, img.rect)
If you need rectangle touching right border of the screen:
img.rect.right = screen.get_rect().right
screen.blit(img.image, img.rect)
If you need rectangle in bottom left corner of the screen:
img.rect.bottomleft = screen.get_rect().bottomleft
screen.blit(img.image, img.rect)
And you have more - see pygame.Rect
x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
Using above element it doesn't change width and height.
If you change x (or other value) then you automaticly get new value of left, right and others.
BTW: As you see you can use img.rect as argument in blit()
BTW: You can also do this: (for example in __init__):
img.rect = img.image.get_rect(center=screen.get_rect().center)
to center object on screen
BTW: You can use it also to blit image/Surface on other Surface at a precise point. You can put text in center of some surface (for example: button) and then that surface put in bottomright corner of the screen
From your code:
screen.blit(img.image, img.rect.topleft)
will put the image at (0, 0) since the rect has been obtained from an image that has not yet been drawn to the display surface. If you want to draw at a specific coordinate simply do this:
screen.blit(image, (x, y)) #x and y are the respective position coordinates