【问题标题】:Can't close the window with button: QML无法使用按钮关闭窗口:QML
【发布时间】:2015-05-07 19:17:04
【问题描述】:

我创建了一个项目,其中包含一个按钮。我正在尝试使用此按钮关闭项目的父窗口,但单击项目时收到此消息:

TypeError: 对象 QQuickRootItem(0x1d8efed8) 的属性“关闭”不是 一个函数

你能帮我解决这个问题吗?

商品代码:

import QtQuick 2.4

Item {

    id: backButton

    ItemForButton{

        id: baseButton
        text: "Back"

        onClicked: {

            backButton.parent.close()
        }

    }

}

窗口代码:

Window {

        id: window
        visible: true
        BackButton {

        }
        x: 30
        y: 30
    }

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    这似乎有点混乱。如果我是你,我会在自定义按钮类型中添加一个点击信号。例如:

    项目:

    import QtQuick 2.4
    
    Item {
        id: backButton
    
        // Add a clicked signal here
        signal clicked()
    
        ItemForButton{
    
            id: baseButton
            text: "Back"
    
            onClicked: {
                // Emit the new clicked signal here:
                backButton.clicked();
            }
        }
    }
    

    窗口:

    Window {
        id: window
        visible: true
    
        BackButton {
            // Respond to the signal here.
            onClicked: window.close();
        }
    }
    

    这为将来以其他方式使用自定义 BackButton 类型提供了灵活性。

    【讨论】:

    • 这个问题没有回答为什么 backButton 的父级不是 Window。我认为应该知道 Window 不是QQuickItem,因此不能是任何QQuickItemparent(属性,而不是函数)。
    • 我已经像你写的那样做了。谢谢你。看来,我的代码由于范围而无法完成。
    猜你喜欢
    • 2022-01-22
    • 2013-04-08
    • 2012-05-14
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多