【问题标题】:QML SwipeView has a opaque background. How can I make it transparent?QML SwipeView 具有不透明的背景。我怎样才能使它透明?
【发布时间】:2017-09-02 04:25:42
【问题描述】:

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

如果我删除 SwipeView,则 red 背景 Rectangle 显示,但只要 SwipeView 存在,它要么是 white 要么 紫色,具体取决于显示的页面。

选择紫色页面:

选择空白页面:

使用 swipeview 和 tabbar 注释掉:

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

【问题讨论】:

    标签: qt colors background qml transparency


    【解决方案1】:

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

    所以你可以这样做:

       Page { background: null }
    

    或:

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

    【讨论】:

    • 我在发布 Q 后几分钟就自己想通了。感谢您对我的温柔!
    • 作为说明,我认为 Page 是强制性的,但如果您愿意,可以使用 Item 代替它。这可能会拯救其他人。
    猜你喜欢
    • 2017-12-11
    • 2020-10-29
    • 1970-01-01
    • 2012-05-26
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多