Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Getting pixel information under cursor

Getting pixel information under cursor

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 657 Views
  • 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.
  • F Offline
    F Offline
    FuzzyWombatPonies
    wrote on last edited by
    #1

    I create an image to display, the mousearea correctly shows the x,y locations. However, I need to extract a single pixel from the image(I am after the RGB values of said pixel) under the cursor. How can I go about doing this?

    Image {
            id: image
            anchors.top: parent.top
            anchors.topMargin: 0
            anchors.left: parent.left
            anchors.leftMargin: 0
            fillMode: Image.Stretch
            source: "file:/home/user/.smah/assets/palette.png"
    
            MouseArea {
                id: mouseArea
                anchors.fill: parent
                        onClicked: {
                            console.log(mouseArea.mouseX + " " + mouseArea.mouseY )
                        }
            }
        }
    
    JonBJ 1 Reply Last reply
    0
    • F FuzzyWombatPonies

      I create an image to display, the mousearea correctly shows the x,y locations. However, I need to extract a single pixel from the image(I am after the RGB values of said pixel) under the cursor. How can I go about doing this?

      Image {
              id: image
              anchors.top: parent.top
              anchors.topMargin: 0
              anchors.left: parent.left
              anchors.leftMargin: 0
              fillMode: Image.Stretch
              source: "file:/home/user/.smah/assets/palette.png"
      
              MouseArea {
                  id: mouseArea
                  anchors.fill: parent
                          onClicked: {
                              console.log(mouseArea.mouseX + " " + mouseArea.mouseY )
                          }
              }
          }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @FuzzyWombatPonies
      I know nothing about QML, but if the x, y you have is a point on a QImage then QRgb QImage::pixel(int x, int y) const returns a QRgb value? Or const uchar *QImage::bits() const if you need to examine a lot of pixels.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        FuzzyWombatPonies
        wrote on last edited by
        #3

        I managed to do the needful using Canvas:

            Canvas {
                id: canvas
                anchors.top: parent.top
                anchors.topMargin: 0
                anchors.left: parent.left
                anchors.leftMargin: 0
                width: 720
                height: 720
                onPaint: {
                    var ctx = getContext("2d")
                    if (canvas.isImageLoaded(rgb_palette)) {
                        var im = ctx.createImageData(rgb_palette);
                        ctx.drawImage(im, 0, 0)
                    }
                }
                Component.onCompleted:loadImage(rgb_palette);
                onImageLoaded:requestPaint();
        
                MouseArea {
                    id: mouseArea
                    anchors.fill: canvas
                    onClicked: {
                        var ctx = canvas.getContext("2d")
                        var id = ctx.getImageData(mouseArea.mouseX, mouseArea.mouseY, 1, 1)
                        console.log(id.data[0], id.data[1], id.data[2])
                    }
                }
            }
        
        1 Reply Last reply
        1

        • Login

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