Important: Please read the Qt Code of Conduct -

Getting mousepressEvent from a pixmap set to a QLabel.

  • Hello Team,
    I need suggestions on creating the label to display an image.

    I have taken a label in the ui form on which I set an image with the help of QPixmap::fromImage() function. I am able to display the image. I also need to display 2 labels displaying some text on that image. I tried 1 approach where i embedded the text in the image and thus the text also got zoomed when the image was zoomed. This should not happen. I need the text to remain same as it is a static text.

    Also I need to capture the mouse press event on the image so that I could show the x,y and the pixel value on my UI.

    Any suggestions how can we achieve both of these.

    Thanks in advance.

  • Lifetime Qt Champion


    Make you own custom widget. That will work the best for this use case.

    You can start with
    To get the mouse support.

    Then you also add a paintEvent function to where you will call base paint to let it paint
    pixmap and then also paint the text on top as you want.
    If you want to move the text as images is scaled you should have some x,y pr text item you can then move if image is changed.
    Since you draw on top of image the text is not scaled with image.

    And then you just use the Creator promote feature to use in your app. ( or new it)
    and it should work super fine

  • @mrjj : Thanks for your reply.
    I just exactly tried the approach to promote to a my custom Label. It worked fine and I am able to capture mouse events.

    But here are some observations.

    1. my image size is 128*32. My Image expands as my label expands which is a correct behavior.
    2. Since my Image size is 128*32, I should get x values(0-127) and y values(0-31). But this is not the case. These values go beyond the image size and also increases as the image expands more.

    Is this because the image expands. If yes, How can I ensure that the x,y values that I get remain within 0-127(width) and 0-31(height)

  • Lifetime Qt Champion


    You mean the x,y of the mouse ?
    Those should be in local coordinate system and be within rect() of the label.

    Please shoe the code as else i have no idea as event->pos is in local as far as i can recall.

    1. Mouse pressEvent

    void DistanceClickableLabel::mousePressEvent(QMouseEvent *event)
    qInfo () << FUNCTION << "Mouse position : " << event->pos();

    1. I set the image using below code.
      m_pPointCloudVisualizerUI->distanceImageLabel->setPixmap( QPixmap::fromImage(myImage).scaled(m_pPointCloudVisualizerUI->distanceImageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

  • Lifetime Qt Champion

    It looks fine.
    So you are saying it shows values bigger than
    DistanceClickableLabel->rect() ?

  • @mrjj

    It is within DistanceClickableLabel->rect() .

    My image size is 12832 which when set on a DistanceClickableLabel also expands as the Label size is more when it is displayed. That size is bigger than 12832.

    I want to understand that does the image size increase , if the label expands. if it does then definitely the the mouse position values will be greater than 128*32.

    I don't wan't that. Even in expanded mode, I want the x,y within 128*32 and the relevant pixel value. How can we achieve this ?

    These are my debugs when the label has been expanded. I click exactly in the center of the image. Below you can see the Image size has increased.

    DistanceClickableLabel::mousePressEvent Mouse position : QPoint(668,77)
    Label Size : QSize(1304, 143)
    Image Size : QSize(572, 143)

  • @mrjj It has been solved.

    I take the label's width and height. I divide the the width by 128 and height by 32. So i get the scale factors for both width and height of the expansion.
    Now i divide the x & y value by these factors and use qceil to round it up. So i get the values between 128*32.

Log in to reply