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. Set alpha component on an existing QML color object
Forum Updated to NodeBB v4.3 + New Features

Set alpha component on an existing QML color object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 3 Posters 9.4k 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.
  • Q Offline
    Q Offline
    QJeroen
    wrote on last edited by
    #1

    Hi,

    I've been searching everywhere but can't find a way to set the alpha component of an existing QML color. I create a color with Qt.hsla(0.08,1,0.53,1) and want to use that color but with a different alpha. Instead of speciying the entire color again I only want to set it's alpha component manually. In C++ this is easy with QColor::setAlpha or QColor::setAlphaF, but in QML I cannot find a function to do this. Since this is such a basic functionality I really think this should be possible and I'm missing something obvious here.

    Please inform me how to do this.

    ODБOïO 1 Reply Last reply
    2
    • Q Offline
      Q Offline
      QJeroen
      wrote on last edited by
      #8

      Thanks for your examples, it gave me the right direction of a good solution. This is what I came up with derive a color from a readonly property:

      function setColorAlpha(color, alpha) {
          return Qt.hsla(color.hslHue, color.hslSaturation, color.hslLightness, alpha)
      }
      
      1 Reply Last reply
      1
      • Q QJeroen

        Hi,

        I've been searching everywhere but can't find a way to set the alpha component of an existing QML color. I create a color with Qt.hsla(0.08,1,0.53,1) and want to use that color but with a different alpha. Instead of speciying the entire color again I only want to set it's alpha component manually. In C++ this is easy with QColor::setAlpha or QColor::setAlphaF, but in QML I cannot find a function to do this. Since this is such a basic functionality I really think this should be possible and I'm missing something obvious here.

        Please inform me how to do this.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by ODБOï
        #2

        @QJeroen hi,

        Window {
            id:root
            visible: true
            width: 640
            height: 480
        
            property real alph : 1
            /*Timer{
                running: true
                repeat: true
                interval: 500
                onTriggered: {
                    if(alph>0.1)
                    alph-=0.1
                    else alph =1
                }
            }*/ 
        
            Rectangle{
                anchors.centerIn: parent
                height: 50
                width: 50
                color: Qt.hsla(0.08,1,0.53,alph)
            }
        }
        
        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          QJeroen
          wrote on last edited by
          #3

          Thanks for your reply, it works but feels a bit like a workaround for me. So apparently there is no function to change the alpha component like: Qt.setAlpha(color, alpha)...

          ODБOïO 1 Reply Last reply
          1
          • Q QJeroen

            Thanks for your reply, it works but feels a bit like a workaround for me. So apparently there is no function to change the alpha component like: Qt.setAlpha(color, alpha)...

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #4

            @QJeroen I don't know if there is a special function to directly change alpha on a color, but this is not 'bad workaround' at all..
            Qt.hsla() is a function witch takes 4 parameters, so you can pass hardParameters ( Qt.hsla(1,1,0.4,1) ) or variables to that function.

            We could do the same thing on 'h, s, l ' parameters to

            property real alph : 1
            property real h : 0.8
            property real s : 1
            property real l : 0.53

            color: Qt.hsla(h,s,l,alph)

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QJeroen
              wrote on last edited by
              #5

              I'm not saying it's bad, but not suited for my case. Let me be more specific of what I'm trying to do.

              I have a singleton qml file which has properties for all the colors that I am using in my application.

              pragma Singleton // load only one per session
              import QtQuick 2.7
              
              QtObject {
                  readonly property color colorBlueDark: Qt.hsla(0.57,1,0.2,1)
              }
              

              Now somewhere in a component, I don't have the exact parameters for every component anymore.

              import "styles"
              
              Rectangle {
                      id: blaat
                      color: {
                          if (pressed)
                              Style.colorBlueDark
                      }
              }
              
              
              ODБOïO 1 Reply Last reply
              0
              • Q QJeroen

                I'm not saying it's bad, but not suited for my case. Let me be more specific of what I'm trying to do.

                I have a singleton qml file which has properties for all the colors that I am using in my application.

                pragma Singleton // load only one per session
                import QtQuick 2.7
                
                QtObject {
                    readonly property color colorBlueDark: Qt.hsla(0.57,1,0.2,1)
                }
                

                Now somewhere in a component, I don't have the exact parameters for every component anymore.

                import "styles"
                
                Rectangle {
                        id: blaat
                        color: {
                            if (pressed)
                                Style.colorBlueDark
                        }
                }
                
                
                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by
                #6

                @QJeroen I see.

                Maybe you can achieve your goal just by changing 'opacity' parameter

                import "styles"

                Rectangle {
                id: blaat
                opacity: 0.2
                color: {
                if (pressed)
                Style.colorBlueDark
                }
                }

                1 Reply Last reply
                0
                • ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by ODБOï
                  #7

                  This is still not exactly what you are looking for, but works also

                  QtObject {
                  id:obj
                  property color colorBlueDark: Qt.hsla(0.57,1,0.2,1)
                  }

                  Component.onCompleted: obj.colorBlueDark.a = 0.9
                  
                  
                  Rectangle{
                      height: 50
                      width: 50
                      anchors.centerIn: parent
                      color : obj.colorBlueDark
                        }
                  

                  Or, i think last possibility is to extend QML with c++ class

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    QJeroen
                    wrote on last edited by
                    #8

                    Thanks for your examples, it gave me the right direction of a good solution. This is what I came up with derive a color from a readonly property:

                    function setColorAlpha(color, alpha) {
                        return Qt.hsla(color.hslHue, color.hslSaturation, color.hslLightness, alpha)
                    }
                    
                    1 Reply Last reply
                    1
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #9

                      You can do color.a = newAlpha

                      Q 1 Reply Last reply
                      0
                      • GrecKoG GrecKo

                        You can do color.a = newAlpha

                        Q Offline
                        Q Offline
                        QJeroen
                        wrote on last edited by
                        #10

                        @GrecKo You can't do that with a readonly property. And if you make a copy of it in javascript that one is also readonly.

                        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