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. How to have a global Gradient elemet which can be used across multiple qml files
Forum Updated to NodeBB v4.3 + New Features

How to have a global Gradient elemet which can be used across multiple qml files

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 4.0k 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.
  • T Offline
    T Offline
    thisisbhaskar
    wrote on last edited by
    #1

    Hi,

    I my application I need same Gradient element across multiple qml files. I don't find a way to define it in one place and use it across multiple qml files. I end up defining same Gradient element in all the qml files.

    Any suggestions on how to solve problem.

    Thanks,
    _Vijay

    1 Reply Last reply
    0
    • L Offline
      L Offline
      ludde
      wrote on last edited by
      #2

      Have you tried defining the gradient as a component, i.e. in a separate QML file starting with a capital letter, e.g. MyGradient.qml?
      Then, you should be able to use MyGradient instead of Gradient, i.e.:

      @gradient: MyGradient {}@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        If you need the same gradient several times, better render it to an image and use that instead. Rendering gradients over and over again is expensive.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thisisbhaskar
          wrote on last edited by
          #4

          Hi Ledde, Yes this works. Thanks for your suggestion.
          But I don't want to have separate qml file for each gradient I have. Want to place them in single qml file and access them when ever I want. That would be a nice thing to do.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            So, instead of MyGradient, create a file GradientProvider instead, and give this one properties that all return a Gradient?

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thisisbhaskar
              wrote on last edited by
              #6

              @If you need the same gradient several times, better render it to an image and use that instead. Rendering gradients over and over again is expensive.@

              Hi Andre, Thanks for your suggestion. What you are suggesting is to run the app in simulator with gradients, and then do a "PrtSc" to copy the gradient part and use that image in the code ? Will the aspect ration and clarity of the image be preserved when I use same image across devices with multiple resolutions ?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                It depends on your gradients and how you use them, I think.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  thisisbhaskar
                  wrote on last edited by
                  #8

                  This is how it looks now in my code..

                  I have a file called GradientProvider.qml

                  @import QtQuick 1.0

                  Item {
                  id: gradientProvider

                  property Gradient darkBlueGradient : darkBlueGradientItem
                  property Gradient lightBlueGradient : lightBlueGradientItem
                  
                  Gradient {
                      id: darkBlueGradientItem
                      GradientStop { color: "darkblue"; position: 0.0 }
                      GradientStop { color: "lightblue"; position: 0.5 }
                      GradientStop { color: "darkblue"; position: 1.0 }
                  }
                  
                  Gradient {
                      id: lightBlueGradientItem
                      GradientStop { color: "lightblue"; position: 0.0 }
                      GradientStop { color: "blue"; position: 0.5 }
                      GradientStop { color: "lightblue"; position: 1.0 }
                  }
                  

                  }@

                  when ever I want darkBlueGradient, I do below

                  @ GradientProvider { id: gradientProvider }@

                  @ gradient : gradientProvider.darkBlueGradient @

                  Does this look ok?

                  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