exo
exo

Reputation: 385

qml mainwindow background gradient

I have such an idea. I want to have a gradient instead of a monochrome background in myApp main window.

I tested different ways of code, but without success.

The result is always the same. Either the background is pure white or the black and white gradient is shown (see picture).

Can you advise me how to do it right? I need something much darker. Moreover, maybe in other colors. But this shows everything only in black and white.

enter image description here

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0

ApplicationWindow {
id: window
visible: true
width: 425
height: 850
title: qsTr("myApp")

/*
Item
{
    Gradient
    {
        GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
        GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
        GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
        GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
        GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
        GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
        GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
    }
}

//totaly not works
Rectangle
{
    Gradient
    {
        GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
        GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
        GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
        GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
        GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
        GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
        GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
    }
}


//totaly not works
Gradient
{
    GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
    GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
    GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
    GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
    GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
    GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
    GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
}


//malfunction - still show only two colors - black nad white
RadialGradient
{
    anchors.fill: parent
    GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
    GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
    GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
    GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
    GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
    GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
    GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
}


//malfunction - still show only two colors - black nad white
Rectangle
{
    anchors.fill: parent
    Gradient
    {
        GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
        GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
        GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
        GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
        GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
        GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
        GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
    }
}


//Totaly not works
Item
{
    Gradient
    {
        GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
        GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
        GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
        GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
        GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
        GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
        GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
    }
}

*/


Item
{
    anchors.fill: parent
    RadialGradient
    {
        anchors.fill: parent
        GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
        GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
        GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
        GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
        GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
        GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
        GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
    }
}

}

Upvotes: 0

Views: 2459

Answers (1)

nayab
nayab

Reputation: 2380

You have to apply list of gradientstops to gradient property of visual item.

To use rectangle inside the Item, one must specify dimensions of Item, by default Item has zero height and width.


import QtQuick 2.9
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0

ApplicationWindow {
id: window
visible: true
width: 425
height: 850
title: qsTr("myApp")


Rectangle
{
    anchors.fill: parent
    gradient: Gradient
    {
        GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
        GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
        GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
        GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
        GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
        GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
        GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
    }
}
}

enter image description here


To use radialgradient

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0

ApplicationWindow {
id: window
visible: true
width: 425
height: 850
title: qsTr("myApp")

Item
{
    anchors.fill: parent
     RadialGradient
    {
        anchors.fill: parent
        gradient: Gradient{
        GradientStop {position: 0.000;color: Qt.rgba(1, 0, 0, 1);}
        GradientStop {position: 0.167;color: Qt.rgba(1, 1, 0, 1);}
        GradientStop {position: 0.333;color: Qt.rgba(0, 1, 0, 1);}
        GradientStop {position: 0.500;color: Qt.rgba(0, 1, 1, 1);}
        GradientStop {position: 0.667;color: Qt.rgba(0, 0, 1, 1);}
        GradientStop {position: 0.833;color: Qt.rgba(1, 0, 1, 1);}
        GradientStop {position: 1.000;color: Qt.rgba(1, 0, 0, 1);}
        }
    }
}
}

enter image description here

Upvotes: 2

Related Questions