qt - QML SwipeView 覆盖整个窗口

标签 qt qml qtquick2 qtquickcontrols qtquickcontrols2

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

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”蓝色区域是可见的,它覆盖了正确的区域(黄色矩形):

enter image description here

2) 如果我点击 移至 2 按钮 然后黄色矩形按预期可见。

enter image description here

现在,即使我点击 移至 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

If clipping is enabled, an item will clip its own painting, as well as the painting of its children, to its bounding rectangle.



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

关于qt - QML SwipeView 覆盖整个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45319828/

相关文章:

c++ - QTableView并设置指针位置

c++ - QtCore 是否也适合服务器端使用 'heavy' ?

QT Ubuntu 窗口处于事件状态但不可见

c++ - 将 Qt::[Enum/Flag] 注册为 QtMetaType

c++ - QML - 实现 C++ ListModel 的最佳方式

c++ - 使用几何着色器实现裁剪平面?

qt - 动态添加到 qml tabbar 和 stacklayout

qml - “Expected token ',在QML中的信号处理程序中使用transition属性时的' ”

qt - 从 QML 组合框传播点击

c++ - 使用 inputMask 时不显示 TextFields 中的 placeholderText