我正在尝试隐藏或删除项目(按钮)
\n这是代码:
\n//when you click on the button I try to delete the button2\nButton {\n id: button2 \n text: qsTr("DRINA")\n\n ToolTip.visible: hovered\n ToolTip.text: qsTr("Save the active project")\n}\nButton {\n id: button3\n text: qsTr("delete")\n\n ToolTip.visible: hovered\n ToolTip.text: qsTr("delete the active project")\n clicked: button2. //alas but it does not offer options like delete and hide\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我还想隐藏或删除 ColumnLayout 类型的元素(我希望这与 Button 元素的操作方式相同)
\nvisible通过将属性设置为 来隐藏按钮false。您也clicked错误地使用了信号。您不应该为信号本身分配任何内容。相反,您可以定义onClicked信号的处理程序。所以你的代码应该是这样的:
Button {
id: button3
text: qsTr("delete")
ToolTip.visible: hovered
ToolTip.text: qsTr("delete the active project")
onClicked: button2.visible = false
}
Run Code Online (Sandbox Code Playgroud)
您无法删除已按照您显示的方式静态定义的 QML 项。你只能隐藏它。如果您更改代码以Button动态创建(例如,使用Loader),则可以将其删除。
Component {
id: dynamicBtn
Button {
// Button that can be deleted
}
}
Loader {
id: btnLoader
sourceComponent: dynamicBtn
}
Button {
// Unload (delete) the other button
onClicked: btnLoader.sourceComponent = undefined
}
Run Code Online (Sandbox Code Playgroud)