【问题标题】:QML SwipeView is covering entire windowQML SwipeView 覆盖整个窗口
【发布时间】:2017-07-26 07:08:16
【问题描述】:

如果我使用滑动视图并且我编写了以下代码,我会遇到一些问题:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Swipe View")

    MainForm {
        anchors.fill:parent

        Rectangle{
            id:rightRect
            anchors.right:parent.right
            width: parent.width*0.50
            height:parent.height
            color:"yellow"
        }

        Rectangle{
            id:leftRect
            width:parent.width*0.50
            height:parent.height
            color:"lightgreen"
            border.color:"red"
            anchors.right:rightRect.left
            SwipeView{
                id:swipeView
                anchors.fill : leftRect
                //Layout.fillWidth: true
                currentIndex: 0
                interactive: false
                Page{
                    id:page1

                    Rectangle{
                        width:parent.width
                        height:parent.height
                        color:"lightgreen"
                        Button{
                            text:"move to 2"
                            onClicked: swipeView.currentIndex = 1
                        }
                    }
                }

                Page{
                    id:page2
                    Rectangle{
                        width:parent.width
                        height:parent.height
                        color:"lightblue"
                        Button{
                            text:"move to 1"
                            onClicked: swipeView.currentIndex = 0
                        }
                    }
                }
            }
        }
    }
}

以下是屏幕截图:

1) 最初我将当前索引设置为 "0" 但索引“1”蓝色区域可见并且它覆盖了正确的区域(黄色矩形):

2) 如果我点击 move to 2 button 则黄色矩形如预期可见。

现在,即使我点击 move to 1 button 我也需要相同的行为,即黄色矩形应该一直可见。如何实现这一点??

【问题讨论】:

    标签: qt qml qtquick2 qtquickcontrols qtquickcontrols2


    【解决方案1】:

    clip:true 添加到您的 SwipeView 的父级,它会很好。

    Rectangle{
                id:leftRect
                width:parent.width*0.50
                height:parent.height
                color:"lightgreen"
                border.color:"red"
                anchors.right:rightRect.left
                clip:true    //add this
                SwipeView{
    

    根据Qt Documentation

    如果启用剪辑,项目将剪辑自己的绘画,以及 它的孩子的绘画,到它的边界矩形。

    默认情况下,此值为 false,因此您的 SwipeView 会超出矩形区域。

    【讨论】:

    • 如果我添加剪辑,它会按预期工作,但是有没有办法在索引更改时禁用转换??
    • 你指的是哪个索引?
    • SwipeView -> 当我设置这个 swipeView.currentIndex 有一些过渡/动画发生。
    • 我相信您的问题已在 Qt 论坛上列出:forum.qt.io/topic/81535/swipeview-page-change-without-animation 不幸的是,仅使用上面链接中提到的 SwipeView 无法做到这一点。
    • @pra7 看看StackLayout,它与SwipeView做同样的事情,但是当你改变currentIndex时没有动画。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多