qml主窗口背景渐变

问题描述 投票:0回答:1

我有这样的想法。我想在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);}
    }
}

}

qt qml qmainwindow
1个回答
1
投票

您必须将gradientstops列表应用于可视项目的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

© www.soinside.com 2019 - 2024. All rights reserved.