QML SwipeView 有一个不透明的背景。我怎样才能让它透明?

QML SwipeView has a opaque background. How can I make it transparent?

QML 中,我用 SwipeView 实现了一个简单的应用程序,如下所示:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2

Window {
    visible: true
    Rectangle{
        id: bg
        anchors.fill: parent
        color: "red"
    }

    SwipeView {
        id: swipeView
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.bottom: tabBar.top
        currentIndex: tabBar.currentIndex

       Page{
        // Empty
       }

        Page{
            Rectangle{
                anchors.fill: parent
                color:"purple"
            }
        }
    }

    TabBar {
        id: tabBar
        width: parent.width;
        height: 30;
        anchors.bottom: parent.bottom
        currentIndex: swipeView.currentIndex
        TabButton {
            text: qsTr("Empty")
        }
        TabButton {
            text: qsTr("Purple")
        }
    }
}

如果我删除 SwipeViewred 背景 Rectangle 显示,但只要 SwipeView 存在,它就是 whitepurple 取决于显示的页面。

选择了紫色页面:

选中空白页:

注释掉 swipeview 和 tabbar:

那么,我怎样才能让红色背景闪耀(即让 SwipeView 透明)?

SwipeView默认是透明的。 Page 实际上不是 "empty"。因为继承Control,所以有背景属性。默认情况下,Page 将 Rectangle 设置为背景。这就是为什么您看到空选项卡为白色的原因。 Page.qml Sourcecode

所以你可以这样做:

   Page { background: null }

或:

    Page {
        background: Rectangle{                
            color:"transparent"
        }
    }