2015-03-31 48 views

回答

9

您可以使用LinearGradient QML類型。

import QtQuick 2.4 
import QtQuick.Window 2.2 
import QtGraphicalEffects 1.0 

Window 
{ 
    visible: true 
    height: 500 
    width: 500 
    Text { 
     id: text 
     font.pointSize: 55 
     anchors.centerIn: parent 
     text: "Hello World!" 
     visible: false 
    } 
    LinearGradient { 
     anchors.fill: text 
     source: text 
     gradient: Gradient { 
      GradientStop { position: 0; color: "yellow" } 
      GradientStop { position: 1; color: "red" } 
     } 
    } 
} 
+0

Upvoted。如果沒有可用的着色器知識,這是最簡單的方法。很奇怪,在十個小時左右沒有人提出這個問題! – BaCaRoZzo 2015-04-01 10:08:38

+1

同意@BaCaRoZzo,這種解決方案更容易,更好的 – folibis 2015-04-01 12:05:51

+0

看起來不工作的設備不支持openGL – 2017-08-30 09:11:02

5

它使用Item layers 例如是可能的:

import QtQuick 2.3 
import QtQuick.Window 2.0 
import QtGraphicalEffects 1.0 

Window { 
    width: 400 
    height: 300 
    visible: true 
    Rectangle { 
     id: gradientRect; 
     width: 10 
     height: 10 
     gradient: Gradient { 
      GradientStop { position: 0; color: "yellow" } 
      GradientStop { position: 1; color: "red" } 
     } 
     visible: false; 
     layer.enabled: true; 
     layer.smooth: true 
    } 

    Text { 
     id: txt 
     anchors.centerIn: parent 
     text: "Hello, world" 
     font.pixelSize: 64 
     layer.enabled: true 
     layer.samplerName: "maskSource" 
     layer.effect: ShaderEffect { 
      property var colorSource: gradientRect; 
      fragmentShader: " 
         uniform lowp sampler2D colorSource; 
         uniform lowp sampler2D maskSource; 
         uniform lowp float qt_Opacity; 
         varying highp vec2 qt_TexCoord0; 
         void main() { 
          gl_FragColor = 
           texture2D(colorSource, qt_TexCoord0) 
           * texture2D(maskSource, qt_TexCoord0).a 
           * qt_Opacity; 
         } 
        " 
     } 
    } 
} 

查一查here更多的例子。