Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    QImage::pixel() in QML

    QML and Qt Quick
    2
    5
    6711
    Loading More Posts
    • 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.
    • G
      guillaume.belz last edited by

      For Qt exercise, I try tro write ColorPicker in QML. I have no problem to create gradients and mouse management : "image on forum":http://www.developpez.net/forums/d965869/c-cpp/bibliotheques/qt/contribuez/exercice-qt-color-picker-premiere-partie/#post5422837

      I want to send signal on click with selected color. But I can't know color of pixel at given position.

      Have you ideas to do this ?

      PS : I have 2 solutions to do this : create specific QObject to store image and pick color and calcul RGBA parameters with x,y position. But I want to know if they are more simple solution

      Thanks

      1 Reply Last reply Reply Quote 0
      • D
        DenisKormalev last edited by

        AFAIK there is no other solution than calculate color from x,y pair

        1 Reply Last reply Reply Quote 0
        • G
          guillaume.belz last edited by

          Thanks for reply

          I do with x,y values

          After the end of exercise, I send link with code here, those who would be interested.

          Thanks

          1 Reply Last reply Reply Quote 0
          • ?
            Guest last edited by

            yes, you could also add a howto wiki page for this

            1 Reply Last reply Reply Quote 0
            • G
              guillaume.belz last edited by

              The code I had write :

              @import Qt 4.7

              Item
              {
              width: 256
              height: 256

              function updateSelectedColor()
              {
                  ColorPickerContext.selected_red =
                          (1 - (cursor.y / height)) *
                          (1 + (cursor.x / width) * (main_red - 1))
                  ColorPickerContext.selected_green =
                          (1 - (cursor.y / height)) *
                          (1 + (cursor.x / width) * (main_green - 1))
                  ColorPickerContext.selected_blue =
                          (1 - (cursor.y / height)) *
                          (1 + (cursor.x / width) * (main_blue - 1))
              }
              
              Rectangle
              {
                  width: parent.height
                  height: parent.width
                  transform: Rotation { angle: 90}
                  x: parent.width
                  y: 0
                  gradient: Gradient
                  {
                      GradientStop { position: 0.0; color: 
                                     Qt.rgba(main_red, main_green, main_blue, 1)}
                      GradientStop { position: 1.0; color: "white" }
                  }
              }
              
              Rectangle
              {
                  anchors.fill: parent
                  gradient: Gradient
                  {
                      GradientStop { position: 0.0; color: Qt.rgba(0, 0, 0, 0) }
                      GradientStop { position: 1.0; color: Qt.rgba(0, 0, 0, 1) }
                  }
              }
              
              Image
              {
                  id: cursor
                  x: width/2
                  y: height/2
                  source: "cursor.png"
              }
              
              MouseArea
              {
                  acceptedButtons: Qt.LeftButton
                  anchors.fill: parent
                  onPressed:
                  {
                      cursor.x = mouseX - 4
                      cursor.y = mouseY - 4
                      updateSelectedColor()
                  }
                  onPositionChanged:
                  {
                      cursor.x = mouseX - 4
                      cursor.y = mouseY - 4
                      updateSelectedColor()
                  }
              }
              

              }@

              I'm not expert in QML, so if someone have critics, I'm interessed.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post