Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting mousepressEvent from a pixmap set to a QLabel.
Forum Updated to NodeBB v4.3 + New Features

Getting mousepressEvent from a pixmap set to a QLabel.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Uday More
    wrote on last edited by
    #1

    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.

    mrjjM 1 Reply Last reply
    0
    • U Uday More

      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.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Uday-More

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

      You can start with
      https://wiki.qt.io/Clickable_QLabel
      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

      1 Reply Last reply
      2
      • U Offline
        U Offline
        Uday More
        wrote on last edited by
        #3

        @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)

        mrjjM 1 Reply Last reply
        0
        • U Uday More

          @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)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Uday-More

          Hi
          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 Reply Last reply
          0
          • U Offline
            U Offline
            Uday More
            wrote on last edited by
            #5
            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));
            mrjjM 1 Reply Last reply
            0
            • U Uday More
              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));
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • U Offline
                U Offline
                Uday More
                wrote on last edited by Uday More
                #7

                @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)

                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  Uday More
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved