qt - 在 SwipeView QML 上动态添加 QML 页面

标签 qt qml qt5 swipeview

我需要在滑动 View 上动态添加页面。 所以我的代码:

    SwipeView {
        id: viewSwipe
        width: parent.width
        height: parent.height * 0.70
        currentIndex: 0
        Component.onCompleted: {
            curIndexWitouthZero = viewSwipe.currentIndex
            curIndexWitouthZero += 1
            addPage(RecipesPhase)
        }

        onCurrentIndexChanged: {
            curIndexWitouthZero = viewSwipe.currentIndex
            curIndexWitouthZero += 1
        }

        Item {
            id: firstPage
            RecipesPhase{}
        }
    }

    PageIndicator {
        id: indicator
        interactive: true
        count: viewSwipe.count
        currentIndex: viewSwipe.currentIndex

        anchors.top: viewSwipe.bottom
        anchors.topMargin: parent.height * 0.05
        anchors.horizontalCenter: parent.horizontalCenter
    }

我有一个按钮,单击该按钮时,事件应该像项目一样添加页面。 我的代码是:

  MouseArea{
     anchors.fill: parent
     onClicked: {
         console.log("Cliccato additem")
         //viewSwipe.addItem(RecipesPhase)
         viewSwipe.addPage(RecipesPhase)
     }
  }

但是什么也没发生。所以我也尝试过:

     SwipeView {
        id: viewSwipe
        width: parent.width
        height: parent.height * 0.70
        currentIndex: 0
        Component.onCompleted: {
            curIndexWitouthZero = viewSwipe.currentIndex
            curIndexWitouthZero += 1
            addPage(RecipesPhase)
        }

        onCurrentIndexChanged: {
            curIndexWitouthZero = viewSwipe.currentIndex
            curIndexWitouthZero += 1
        }

        function addPage(page) {
            console.log("funzione addPage()")
           addItem(page)
           page.visible = true
        }
    }

然后调用:

viewSwipe.addPage(MyPageQML)

但是什么也没发生。 那么问题来了,我哪里错了?感谢您提供解决方案。

最佳答案

RecipesPhase 是一个组件而不是一个 Item,组件类似于类,Item 类似于对象,所以想法是动态创建项目。例如,我会认为 RecipesPhase 是以下组件:

RecipesPhase.qml

import QtQuick 2.0

Rectangle {
    width: 100
    height: 100
    color: "red"
}

然后每次按下 mouseArea 时,您都会创建一个随机颜色的 RecipesPhase:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Example")
    property int curIndexWitouthZero: 0

    SwipeView {
        id: viewSwipe
        width: parent.width
        height: parent.height * 0.70
        currentIndex: 0
        Component.onCompleted: {
            curIndexWitouthZero = viewSwipe.currentIndex
            curIndexWitouthZero += 1
            addPage(createPage())
        }

        onCurrentIndexChanged: {
            curIndexWitouthZero = viewSwipe.currentIndex
            curIndexWitouthZero += 1
        }

        function addPage(page) {
            console.log("funzione addPage()")
            addItem(page)
            page.visible = true
        }
        function createPage(){
            var component = Qt.createComponent("RecipesPhase.qml");
            var page = component.createObject(viewSwipe,
                                              {"color": Qt.rgba(Math.random(), Math.random(), Math.random(), Math.random())}
                                              );
            return page
        }
    }
    PageIndicator {
        id: indicator
        interactive: true
        count: viewSwipe.count
        currentIndex: viewSwipe.currentIndex
        onCurrentIndexChanged: viewSwipe.currentIndex = currentIndex

        anchors.top: viewSwipe.bottom
        anchors.topMargin: parent.height * 0.05
        anchors.horizontalCenter: parent.horizontalCenter
    }
    MouseArea{
        anchors.top: indicator.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        onClicked: viewSwipe.addPage(viewSwipe.createPage())
    }
}

关于qt - 在 SwipeView QML 上动态添加 QML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53394259/

相关文章:

c++ - QT5 左对齐 OSX QTabWidget

c++ - Qt - 拖动标题栏时防止窗口调整大小

python - 将 QWebView 中的 Javascript 异常打印到控制台

linux - QFontDatabase::addApplicationFont 适用于嵌入式 Linux,但不适用于 Linux Ubuntu

c++ - 使用 CMake 创建 Qt qml C++ 插件

html - 带有 <hr> 行的 QML 文本

javascript - 引用错误: Float64Array is not defined - Javascript with QML

c++ - 使用 Qt 编写安全的服务器应用程序

qt - 无法通过 SplitView 中的 id 访问 QML 项目

QML TreeView 的 C++ 模型