Qt Quick Esempi - Effetti shader
Un esempio di Qt Quick che illustra l'uso degli effetti shader.

Questo esempio illustra alcuni effetti visivi che è possibile ottenere con gli shader in Qt Quick. Applica cinque effetti diversi a un testo e a un paio di immagini. Per ulteriori informazioni, visitare la pagina Concetti importanti in Qt Quick - Graphical Effects
Esecuzione dell’esempio
È possibile eseguire l'esempio da:
- Qt Creator
Aprire la modalità " Welcome " e selezionare l'esempio da Examples. Per ulteriori informazioni, consultare Qt Creator: Tutorial: Compilazione ed esecuzione.
- Qt Extension for Visual Studio Code
Eseguire il comando Qt: Open Qt examples da Command Palette e selezionare l’esempio dall’elenco. Per ulteriori informazioni, consultare Qt Extension for Visual Studio Code: Tutorial: Compilazione ed esecuzione.
Utilizzo di ShaderEffect
Il tipo ` ShaderEffect ` opera in genere su altri tipi, utilizzando una ` ShaderEffectSource`:
ShaderEffectSource {
id: theSource
sourceItem: theItem
}Nel frammento di codice sopra riportato, ` theItem ` è l’ID di un oggetto QML complesso presente nel file.
Gli ShaderEffect possono utilizzare questo ShaderEffectSource come texture nel proprio frammento di shader:
fragmentShader: "content/shaders/wobble.frag.qsb"Per supportare più API grafiche, non solo OpenGL, il codice sorgente dello shader non è incorporato nel QML. Il file .qsb a cui si fa riferimento è un pacchetto di shader pregenerato contenente diverse varianti del codice dello shader. Lo shader appropriato viene quindi scelto Qt Quick in fase di esecuzione, a seconda dell’API grafica (Vulkan, Metal, Direct3D 11 o OpenGL) utilizzata in quel momento. Il file .qsb viene generato offline e integrato nell’eseguibile tramite il Qt Resource System.
È possibile utilizzare qualsiasi proprietà personalizzata dell’ ShaderEffect nel proprio shader. Ciò semplifica notevolmente la creazione di codice shader animato:
property variant source: theSource
property real bend
property real minimize
property real side: genieSlider.value
SequentialAnimation on bend {
loops: Animation.Infinite
NumberAnimation { to: 1; duration: 700; easing.type: Easing.InOutSine }
PauseAnimation { duration: 1600 }
NumberAnimation { to: 0; duration: 700; easing.type: Easing.InOutSine }
PauseAnimation { duration: 1000 }
}
SequentialAnimation on minimize {
loops: Animation.Infinite
PauseAnimation { duration: 300 }
NumberAnimation { to: 1; duration: 700; easing.type: Easing.InOutSine }
PauseAnimation { duration: 1000 }
NumberAnimation { to: 0; duration: 700; easing.type: Easing.InOutSine }
PauseAnimation { duration: 1300 }
}ShaderEffects può anche disporre di uno shader per vertici personalizzato. Impostando la proprietà mesh su ShaderEffect si ottengono più vertici da manipolare, consentendo di realizzare un maggior numero di effetti.
mesh: Qt.size(10, 10)
vertexShader: "content/shaders/genie.vert.qsb"© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.