如何删除或隐藏对象?

tim*_*256 0 qml

我正在尝试隐藏或删除项目(按钮)

\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}\n
Run Code Online (Sandbox Code Playgroud)\n

\xd0\xb2\xd0\xb2\xd0\xb5\xd0\xb4\xd0\xb8\xd1\x82\xd0\xb5\xd1\x81\xd1\x8e\xd0\xb4\xd0\xb0\xd0\xbe\xd0 \xbf\xd0\xb8\xd1\x81\xd0\xb0\xd0\xbd\xd0\xb8\xd0\xb5 \xd0\xb8\xd0\xb7\xd0\xbe\xd0\xb1\xd1\x80\xd0\xb0 \xd0\xb6\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f

\n

我还想隐藏或删除 ColumnLayout 类型的元素(我希望这与 Button 元素的操作方式相同)

\n

Jar*_*Man 5

visible通过将属性设置为 来隐藏按钮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)