不推荐将参数注入信号处理程序。使用带有形式参数的 JavaScript 函数

aar*_*ich 6 qt qml

我有以下问题。

我正在将 QML 用于我的应用程序前端。

对于我的一个组件,我使用一个 ListView,它使用一个自定义委托,该委托在名为 vmIndex 的变量中包含它自己的索引(和一些其他数据)。这是列表视图的声明:

                ListView {
                    id: studySelectListView
                    anchors.fill: parent
                    model: studySelectList
                    delegate: VMStudyEntry {
                        width: studySelectBackground.width
                        height: studySelectBackground.height/4
                        onItemSelected: {
                            selectionChanged(vmIndex,true); // LINE WITH WARNING
                        }
                    }
                }
Run Code Online (Sandbox Code Playgroud)

现在,当选择该项目时,我需要调用一个名为 SelectionChange 的函数并将 vmIndex 作为参数发送给该函数。

VMStudyEntry 是这样的:

Item {
    id: vmStudyEntry

    signal itemSelected(int vmIndex);

    MouseArea {
        anchors.fill: parent
        onClicked: {
            vmStudyEntry.itemSelected(vmIndex);
        }
    }
    Rectangle {
        id: dateRect
        color: vmIsSelected? "#3096ef" : "#ffffff"
        border.color: vmIsSelected? "#144673" : "#3096ef"
        radius: mainWindow.width*0.005
        border.width: mainWindow.width*0.0005
        anchors.fill: parent
        Text {
            font.family: viewHome.gothamR.name
            font.pixelSize: 12*viewHome.vmScale
            text: vmStudyName
            color: vmIsSelected? "#ffffff" : "#000000"
            anchors.centerIn: parent
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这一切在 Qt 5.13.2 上都完美运行。但我现在转向了 Qt 6.1.2。据我所知,代码实际上仍然有效,但我收到此警告:

Parameter "vmIndex" is not declared. Injection of parameters into signal handlers is deprecated. Use JavaScript functions with formal parameters instead.
Run Code Online (Sandbox Code Playgroud)

上面标识了具有此警告的行。谁能告诉我如何重新定义它以使这个警告消失?

Amf*_*sis 15

看一下这里,在Qt6中需要以下语法:

onItemSelected: function(vmIndex) { selectionChanged(vmIndex,true); }
Run Code Online (Sandbox Code Playgroud)

或者如果您喜欢的话,可以使用更像 lambda 的选项:

onItemSelected: vmIndex => selectionChanged(vmIndex, true)
Run Code Online (Sandbox Code Playgroud)