qml主窗口背景渐变

时间:2018-09-18 06:48:25

标签: qt qml qmainwindow

我有这样的主意。 我想在myApp主窗口中使用渐变色而不是单色背景。

我测试了不同的代码方式,但没有成功。

结果始终相同。背景要么是纯白色,要么显示了黑白渐变(请参见图片)。

您能告诉我正确的做法吗?我需要更黑的东西。此外,也许还有其他颜色。但这只能以黑白显示所有内容。

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);}
    }
}

}

1 个答案:

答案 0 :(得分:1)

您必须将渐变停止列表应用于视觉项的gradient属性。

要在Item内使用矩形,必须指定Item的尺寸,默认情况下Item的高度和宽度为零。


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


要使用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