首页 > 解决方案 > QML SwipeView 同时显示所有项目

问题描述

当我在SwipeView中添加带有文本的项目时,它向我显示所有项目都在同一个位置,如果我正在寻找预览SwipeView运行良好。

代码:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    SwipeView {
        id: swipeView
        x: 195
        y: 133
        width: 200
        height: 200
        interactive: true
        currentIndex: 1
        clip: ture

        Item {
            id: hello

            Text {
                id: test
                text: qsTr("Hello")                    
            }
        }

        Item {
            id: bye

            Text {
                id: qrr
                text: qsTr("Bye")
            }
        }
    }
}

这就是我得到的: 图片

标签: qtqmlswipeview

解决方案


您需要为 SwipeView 设置锚点如下所示:

SwipeView {
    id: swipeView
    anchors.fill: parent
}

完整代码:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    SwipeView {
        id: swipeView
        currentIndex: 1
        anchors.fill: parent

        Item {
            Text {
                text: qsTr("Hello")
            }
        }

        Item {
            Text {
                text: qsTr("Bye")
            }
        }
    }
}

推荐阅读