QML gradients with orientation

QML gradient only allows top to bottom in rectangle. The documentation says this should be done with a combination of rotation and clipping.

I've just started learning QML (and some HTML / CSS experience). Here is my code, which I think could be improved much better:

import QtQuick 1.0

Rectangle {
    width: 400; height: 400;

    Rectangle {
    width: 567; height: 567;

    gradient: Gradient {
            GradientStop {
        position: 0.0;
        color: "white";
        }
            GradientStop {
        position: 1.0;
        color: "blue";
        }
    }
    x: 116.5;
    transformOrigin: Item.Top;
    rotation: 45.0;
    }
}

      

Could you suggest which are the best ways to do this?

+6


source to share


3 answers


I solved this problem with the following code: https://code.google.com/p/ytd-meego/source/browse/trunk/playground/FeedBook/qmltube/HorizontalGradient.qml?r=144

Here's what I did using NielsMayer's example:



    Rectangle {
        width: parent.height
        height: parent.width
        anchors.centerIn: parent
        rotation: 90

        gradient: Gradient {
            GradientStop { position: 0.0; color: "black" }
            GradientStop { position: 1.0; color: "white" }
        }
    }

      

And it works well. Enjoy!

+8


source


The Qt Graphical Effects module, introduced in Qt 5.1, provides three types of gradients. With the LinearGradient

(effect) element, it is no longer necessary to apply rotation to achieve, for example, a horizontal color gradient.
In particular, using start

and end

point (attributes) LinearGradient

any direction of the gradient.

The following code implements a 45 ° angle (as suggested in the original post), starting at the top-right corner in white and ending at the bottom-left in black:



import QtQuick 2.0
import QtGraphicalEffects 1.0

Item {
    id:myItem
    width: 300
    height: 300

    LinearGradient {
        anchors.fill: parent
        start: Qt.point(myItem.width, 0)
        end: Qt.point(0, myItem.height)
        gradient: Gradient {
            GradientStop { position: 0.0; color: "white" }
            GradientStop { position: 1.0; color: "black" }
        }
    }
}

      

+5


source


I'm afraid the documentation is correct. The only other way I can think of is to write a custom QML component in C ++ that does what you want.

If you have knowledge of Qt / C ++ you can start here:

A rectangle can be a good template or a base class:

+3


source







All Articles