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. [SOLVED] Is it possible to conditionally handle a gradient
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Is it possible to conditionally handle a gradient

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 3.8k Views 1 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.
  • W Offline
    W Offline
    winkle100
    wrote on last edited by
    #1

    I've added a gradient to a QML button that I've created for use in other areas of our UI. I need to handle the case where the color of the button is transparent. Is it possible to conditionally display a gradient. When I set the rectangle's color to transparent in the QML designer the gradient is completely removed. Is it possible to do this in the QML?

    Here's an example of my button:

    @

    Rectangle {
    id: rectangle3
    width: rectangle1.width + 20
    height: 462
    color: "#b18b60"

    Rectangle {
        id: rectangle1
        width: 241
        height: 100
        color: "#390dc8"
        anchors.top: parent.top
        anchors.topMargin: 7
        anchors.left: parent.left
        anchors.leftMargin: 10
    

    //
    // Here I would like to say...
    // if ( !color is transparent ) {

        gradient: Gradient {
            GradientStop { position: 0.00; color: "black" }
            GradientStop { position: 0.50; color: "white" }
            GradientStop { position: 1.00; color: "black" }
        }
    

    // }
    }
    }

    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      task_struct
      wrote on last edited by
      #2

      Hi,

      according to documentation of Rectangle

      bq. If both a gradient and a color are specified, the gradient will be used.

      So you can switch between color and gradient, setting gradient to null.

      See this example:

      @
      import QtQuick 2.0

      Rectangle {
      id: rect
      width: 100
      height: 100

      color: "blue"
      
      gradient: Gradient {
          id: grad
          GradientStop { position: 0.0; color: "red" }
          GradientStop { position: 0.33; color: "yellow" }
          GradientStop { position: 1.0; color: "green" }
      }
      
      MouseArea {
          anchors.fill: parent
          onClicked: {
              console.debug(rect.gradient)
              if( rect.gradient != null ) {
                  rect.gradient = null
              }
              else
              {
                  rect.gradient = grad
              }
          }
      }
      

      }
      @

      "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

      • Linu...
      1 Reply Last reply
      0
      • W Offline
        W Offline
        winkle100
        wrote on last edited by
        #3

        Thank you+

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bayang
          wrote on last edited by
          #4

          It worked for me. :)

          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