Qt World Summit: Submit your Presentation

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

  • Lifetime Qt Champion


    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?

  • Lifetime Qt Champion

    Can you show us how you done as mousePress event override directly in the MiniMap
    should work and have top,left as 0,0

  • Sure, here you go

    void MiniMap::mousePressEvent(QMouseEvent* event)
    	int mouse_x = event->pos().x() ;
    	int mouse_y = event->pos().y();

    Is it needed to pass the event on to the parent?

  • Lifetime Qt Champion


    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_y

    they are not in local coordinates?

    I tested with

    class ClickImage : public QLabel
        explicit ClickImage(QWidget *parent = nullptr)
            : QLabel(parent) {}
        ~ClickImage() {}
        void clicked(int x, int y);
        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 ?

  • Sorry but what do you mean by adjusting the logic? The only thing this class does or add can be seen in my initial post. Does adding stretching/spacing mess with the coordinates? I appreciate your effort though.

  • Lifetime Qt Champion

    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 ?

  • The top left corner of the image starts at 10, 10. It generally seems like that there is a ~10 pixel wide border around the image. And no the image doesnt have that border so thats not the problem.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    Also notice the layout_ has a margin of 9 pr default.

    layout_->setContentsMargins(0,0,0,0); to be sure its not that.

  • Edit: @mrjj Thank you so much, that was the issue. How would you know that though...

    Also how am i leaking pixmap memory? I free the pixmap in the destructor which isnt shown here.

  • Lifetime Qt Champion

    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");

    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.

Log in to reply