qml中如何使用SwipeView的事件?

How to use events of SwipeView in qml?

我创建了一个新的 qt quick controls 2 项目。它有一个默认的 SwipeView

现在我想使用 SwipeViewonIndexChanged 事件,但出现错误

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

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

    Page1 {
    }

    Page {
        Label {
            text: qsTr("Second page")
            anchors.centerIn: parent
        }
    }
    onIndexChanged: {  //error is in this line
        console.log(index)
    }
}

错误

qrc:/main.qml:25 Cannot assign to non-existent property "onIndexChanged"

SwipeView没有index这样的属性。你的意思可能是 currentIndex.

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

SwipeView {
    // ...
    onCurrentIndexChanged: {
        console.log(currentIndex)
    }
}