代码之家  ›  专栏  ›  技术社区  ›  Mr. Developer

在SwipeView QML上动态添加QML页

  •  1
  • Mr. Developer  · 技术社区  · 6 年前

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

        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)
    

    但什么也没发生。 所以,问题是,我哪里错了?谢谢你的解决方案。

    1 回复  |  直到 6 年前
        1
  •  1
  •   eyllanesc Yonghwan Shin    6 年前

    RecipeSphase是一个组件而不是一个项目,一个组件类似于一个类,一个项目类似于一个对象,所以其思想是动态地创建项目。例如,我将考虑recipesphase是以下组件:

    接收相位.qml

    import QtQuick 2.0
    
    Rectangle {
        width: 100
        height: 100
        color: "red"
    }
    

    然后,每次按下鼠标指针,都会创建一个随机颜色的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())
        }
    }