Qpixmap transform problem
-
I am trying to translate QPixmap object (an icon) using methods of QPainter class. Related code is:
marker = new QLabel(mpMapCanvas); QPixmap pixmap = QPixmap(":/mapMarker.png"); marker->move(50,50); QSize size = pixmap.size(); QPixmap pixmapc = pixmap; pixmapc.fill(QColor::fromRgb(100, 100, 100, 100)); // gray area, for visualization painter = new QPainter(&pixmapc); QTransform transform; transform.translate(-size.width()/2, 0); painter->setTransform(transform); painter->drawPixmap(0,0, pixmap); marker->setPixmap(pixmapc); marker->show();
before translation:
after translation:
I tried to scale the gray area to prevent the cutting effect but it grows towards to bottom-right:
How can I scale towards to top left to prevent the cutting? I think I need to to change the place of the origin and perform translation using the new origin. How can I do that? Or is there another way to prevent this cutting effect?
-
You only do a translation but not scale. What exactly are you trying to do?
-
You only do a translation but not scale. What exactly are you trying to do?
@Christian-Ehrlicher
Sorry if my question was not clear.I am trying to put a marker on a map. When I click, I can put the marker, but I want to match the bottom tip of the marker/icon with the place where I click. Now the most top left of the icon image (24x24) matches with where I click. In the following figure, the center of the black plus (mouse pointer) indicates where I click :
I want to match the bottom tip of the icon with the center of this black plus, basically. But when I translate it to there, the icon becomes invisible, i.e. it is completely cut/lost.
-
In your case, I think you want to move your whole QLabel by -width/2,-height; the QLabel's pixmap is fine as it is in the original screenshot. Not sure if you will need to do any coordinate transformations to position it exactly how you want. QGraphicsScene/QGraphicsView may be a better option too depending on what you are needing to display and manipulate.
-
In your case, I think you want to move your whole QLabel by -width/2,-height; the QLabel's pixmap is fine as it is in the original screenshot. Not sure if you will need to do any coordinate transformations to position it exactly how you want. QGraphicsScene/QGraphicsView may be a better option too depending on what you are needing to display and manipulate.
@mchinand
Exactly, I want to move the whole label whose size is 24x24 by -width/2,-height, but it somehow moves only the icon and keeps the frame (gray area).My purpose is to mark a specific coordinate (lat, lon). When I click on a point on the map, I can take the corresponding coordinate, and I want to put a marker there. I also thought to add the marker as an attribute but could not manage it...
-
In your case, I think you want to move your whole QLabel by -width/2,-height; the QLabel's pixmap is fine as it is in the original screenshot. Not sure if you will need to do any coordinate transformations to position it exactly how you want. QGraphicsScene/QGraphicsView may be a better option too depending on what you are needing to display and manipulate.
Hi @mchinand, sorry to bother you, but I want to ask a follow-up question. I could manage that problem by using QGraphicsScene/QGraphicsView. Thank you for the suggestion.
When zooming in, zooming out or panning the map, the QGraphicsPixmapItem item is shifting, although I set its position already. I called the flag ItemIgnoresTransformations, but it didn't work. Do you have a recommendation for this?