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. Define image positioning in the QLabel
Forum Updated to NodeBB v4.3 + New Features

Define image positioning in the QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 716 Views 3 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.
  • M Offline
    M Offline
    MAX001
    wrote on last edited by
    #1

    I wanna define image positioning in the QLabel, how i can calculate position?
    I have a QLabel and insert different dimension images and when i click on image it should return a color of the pixel like Color Picker should works. Now it clicks defines but with wrong pixel click.
    Some people advice me use indent and margin functions for QLabel but how i don't really understand.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Research layouts. QLabel is indirectly derived from QWidget, which supports the layout mechanism.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        How did you implement your color picking code ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          How did you implement your color picking code ?

          M Offline
          M Offline
          MAX001
          wrote on last edited by MAX001
          #4

          @SGaist It's main function I control mouse move and Left button click and transfer that pixel to converter to color name.
          And also scale the frameCopy convert to qimage format and insert in grid
          Also is a problem with while True loop it's infinitive and should use with extra window OpenCv to terminate process by ESC button

          def main_image_color(image_path, grid_image_detect):
              global frame
              rootDirectory = os.path.dirname(__file__)
              #image_path = os.path.join(rootDirectory, "Media", "photo5.jpg")
              frame = cv.imread(image_path)
              height, width, _ = frame.shape
              bytesPerLine = 3 * width
          
              # Create a QLabel to display the image
              load_image = QLabel()
              load_image.setAlignment(Qt.AlignCenter)
              grid_image_detect.addWidget(load_image, 0, 2)
          
              # Connect the mousePressEvent function to the mousePressEvent signal
              load_image.installEventFilter(load_image)
              load_image.setMouseTracking(True)
          
              def eventFilter(source, event):
                  if event.type() == QEvent.Type.MouseButtonPress and source is load_image:
                      return mouseMoveEvent(event)
                  return QLabel.eventFilter(load_image, source, event)
          
              load_image.eventFilter = eventFilter
          
              def mouseMoveEvent(event):
                  global clicked_color
                  pos = event.pos()
                  # Get the color of the pixel at the cursor position
                  x = int(pos.x() * width / 800)
                  y = int(pos.y() * height / 500)
                  if event.type() == QEvent.Type.MouseButtonPress:
                      clicked_color = frame[y, x]
                  print("Color at pixel", x, y, ":", clicked_color)
                  return QLabel.mouseMoveEvent(load_image, event)
          
              load_image.mouseMoveEvent = mouseMoveEvent
          
          
              while True:
                  global frameCopy
                  # Redraw the entire image with the new text
                  frameCopy = frame.copy()
                  if clicked_color is not None:
                      color_name = convert_rgb_to_names((int(clicked_color[2]), int(clicked_color[1]), int(clicked_color[0])))
          
                      text_size = cv.getTextSize(color_name, cv.FONT_HERSHEY_SIMPLEX, 2, 5)[0]
                      # Draw a rectangle behind the text
                      text_width, text_height = text_size[0], text_size[1]
                      text_x = int(width/2 - text_width/2)
                      text_y = int(height/2 + text_height/2)
                      cv.rectangle(frameCopy, (text_x - 10, 115), (text_x + text_width + 7, 10 + text_height - 5), (255, 255, 255), -1)
                      cv.putText(frameCopy, color_name, (text_x, 100), 0, 2, (0, 0, 0), 7)
                      cv.putText(frameCopy, color_name, (text_x, 100), 0, 2, (int(clicked_color[0]), int(clicked_color[1]), int(clicked_color[2])), 5)
          
                  
                  q_image = QImage(frameCopy, width, height, bytesPerLine, QImage.Format_RGB888).rgbSwapped()
                  detected_image = QPixmap.fromImage(q_image)
                  load_image.setPixmap((detected_image.scaled(800, 500, Qt.AspectRatioMode.KeepAspectRatio)))
                  load_image.setAlignment(Qt.AlignmentFlag.AlignCenter)
                  grid_image_detect.addWidget(load_image, 0, 2)
          
                  cv.imshow('Project', frameCopy)
                  cv.namedWindow("Project")
                  cv.setMouseCallback('Project', color_detecting)
                  key = cv.waitKey(1)
                  if key == 27:
                      break
              cv.destroyAllWindows()
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Since you are using OpenCV to show your results as well as do the modifications on your images, why not implement your color picker directly with it ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            0
            • SGaistS SGaist

              Since you are using OpenCV to show your results as well as do the modifications on your images, why not implement your color picker directly with it ?

              M Offline
              M Offline
              MAX001
              wrote on last edited by
              #6

              @SGaist I created something like that using OpenCv but the main problem was, i can't click on image inside app.
              Now is the problem with infinitive loop and not realistic dimension of the image it is as QLabel size.

              1 Reply Last reply
              0

              • Login

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