QLabel Pixmap doesnt match with true size
-
Hi,
I want to implement a minimap into my application and have the following code for that
layout_ = new QBoxLayout(QBoxLayout::TopToBottom, this); p = new QPixmap("minimap.png"); l = new QLabel(); l->setPixmap(*p); layout_->addWidget(l, 1, Qt::AlignCenter);
Via mousePressEvent i can now click on the minimap and change my position on the real map. However i noticed that the point 0,0 in the minimap widget is not part of the image. (and so are other points likewise). Is there a way to fix this such that the 0,0 coordinate is the top left point of the image?
-
Hi,
Can you show what you get ?
-
@Infestor said in QLabel Pixmap doesnt match with true size:
point 0,0 in the minimap widget is not part of the image.
There might be some margin / border.
Btw: What coordinate system do you use, when you print out the point, where you have clicked?
The result you get could be in parent coordinates. -
@Pl45m4 Yes i think thats the case too. I have a layout that adds the minimap. However i thought that implementing the mousePressEvent in the minimap widget would yield the minimap coordinates and not the parent ones. Is there a way to fix this?
-
Hi
It's often best to call the base class when you override a function but
since QLabel don't use it for any features, it's not needed in this
case. ( but not hurt either)so if you qDebug()
mouse_x and mouse_ythey are not in local coordinates?
I tested with
class ClickImage : public QLabel { Q_OBJECT public: explicit ClickImage(QWidget *parent = nullptr) : QLabel(parent) {} ~ClickImage() {} signals: void clicked(int x, int y); protected: void mousePressEvent(QMouseEvent *event) override { int mx = event->pos().x(); int my = event->pos().y(); qDebug() << mx << "-" << my; emit clicked(mx, my); } };
and the mx and my are as expected so not sure why it would be for you.
Are you sure you adjusted the logic to now be inside the minimap ?
-
@Infestor
Nope, if the widget is shown(visible) it should always be correct.By adjusting logic i mean if you fixed the offset before and then move code, then
that part would no longer be needed.So if you print out your mouse_x and mouse_y what do they show ?
-
Did you set the label size to the size of the pixmap? I don't see this so the label may be bigger than the pixmap. Also you're leaking the QPixmap instance - no need to create a pointer here.
-
Hi
Also notice the layout_ has a margin of 9 pr default.layout_->setContentsMargins(0,0,0,0); to be sure its not that.
-
@Infestor
Super
well i have had other cases with mysterious gaps and your "~10 pixel wide border " reminded me.Well then you dont leak it then but you also don't need to as QPixmap is meant to be copied (and its cheap)
QPixmap p("minimap.png");
l->setPixmap(p);is better as then we cant forget to delete the pixmap and setPixmap copies it anyway.
When ever you need to use * with Qt classes you give to other Qt class, its a sign you shouldn't need to new it.