qml界面参数传递、返回
时间:2023-05-17 21:37:00
背景
:设计界面时,需要在键盘上设置值,设置返回时需要知道返回值是什么。实现上下级关系可能很方便,但会重复加载键模块,感觉不好。stackView.push(url,{初始值,设定后的值})。
1 我想用参数信号传输,因为两个模块是平等的,上级菜单不识别参数名,报错。
2 使用另一种在线参考资料
https://blog.csdn.net/happyrabbit456/article/details/56670809?
(以StackView以管理页面为例)
(1)page1.qml跳转到page2.qml传值
page1.qml
Rectangle
{
id:rect1
…
MouseArea {
id: maStartQuery
anchors.fill: parent
onClicked:
{
if(!stackView.busy)
stackView.push(Qt.resolvedUrl(“qrc:///qml/Numberkey.qml”),
{number:(100)page2.qml的number对于键盘显示的初始值,您还需要获得按钮后的值,
}
}
…
}
Numberkey.qml定义如下
Rectangle
{
id:rect2
…
property int number:""///要传的值
…
}
(2)Numberkey.qml点击"确定"按钮时返回结果page1.qml
A.在page1.qml添加函数clickedfunc,当点击page2.qml中"确定"按钮调用;
B.在Numberkey.qml添加属性containerqml,用来记录page1.qml;
C.在从page1.qml跳转到Numberkey.qml时,将rect1传给Numberkey.qml的containerqml属性。
page1.qml
Rectangle
{
id:rect1
…
label{
text:100
}
MouseArea {
id: maStartQuery
anchors.fill: parent
onClicked:
{
if(!stackView.busy)
stackView.push(Qt.resolvedUrl(“qrc:///qml/Numberkey.qml”),
{number:100,containerqml:rect1})
}
}
//当点击Numberkey.qml中"确定"按钮调用此函数page1中定义
function clickedfunc(temp)
{
label.text= temp
stackView.pop()
}
…
}
page2.qml
Rectangle
{
id:rect2
…
property variant containerqml: null
property string name:""///要传的值
…
MouseArea {
id: btnOK
anchors.fill: parent
onClicked:
{
containerqml.clickedfunc(200)page1.qml中函数实现了返回值。
}
}
}
追加方法
还有一种
```cpp Page { width: 800 height: 480 id:settingpage title: qsTr("设置") Rectangle { color: "#333" anchors.fill: parent } header: Rectangle { width: parent.width height: 40 Button{ text:"<-" onClicked: mainStack.pop() } } property int value Component{ id:number_input NumberKey{ id:input title: "预置张数" Component.onCompleted: input.number=value Component.onDestruction: { value=input.number console.log("Destruction Beginning!") } } }
注意上面的Component.onCompleted:在建立模块时执行一次。
Component.onDestruction:在模块销毁时执行一次,在这个例子中pop后执行