Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QImage::pixel() in QML



  • 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



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



  • 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



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



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


Log in to reply