如何在QML中传递信号和创建连接

时间:2013-01-04 06:41:02

标签: qt qml

我想将信号从一个qml文件传递到另一个qml文件。因此,当它获得信号时,我可以使另一个文件可见 这是我的main.qml

import QtQuick 1.1

Rectangle{
    id:main
    width:480
    height:272
    gradient: Gradient {
        GradientStop { position: 0.0; color: "light blue" }
        GradientStop { position: 1.0; color: "blue" }
    }
    Welcome{
        id:welcomePage
        width:parent.width
        height:parent.height
        visible:true
    }
    LoginPage{
        id:login
        width:parent.width
        height:parent.height
        visible:false
    }
    Connections{
        ignoreUnknownSignals: true
        onsigLogin:{welcomePage.visible=false
            login.visible=true
        }

    }

}

这是我的welcome.qml

import QtQuick 1.1

Rectangle{
    id:welcome
    width:480
    height:272
    signal sigLogin()

    gradient: Gradient {
        GradientStop { position: 0.0; color: "light blue" }
        GradientStop { position: 1.0; color: "blue" }
    }
    Text{
        text:"\n\t\tPRESS ENTER"
        font.bold:true
        font.pointSize: 17
    }
    Button {
        id: wel
        height:30;
        x:parent.width/2-30
        y:parent.height/2-30
        focus:true
        border.color:"black"
        opacity: activeFocus ? 1.0 : 0.5
        Text{
        text:"WELCOME"
            anchors.horizontalCenter:wel.horizontalCenter;
            anchors.verticalCenter:wel.verticalCenter;
        }
        Keys.onReturnPressed: {
            wel.focus=false
            welcome.sigLogin()
        }
    }
}

当我运行时,我收到以下错误

file:///home/sakshi/try1/main.qml:24:9: Cannot assign to non-existent property    "onsigLogin" 
         onsigLogin:{welcomePage.visible=false 
         ^ 

任何人都可以建议我如何从一个文件传递信号以及如何进行更改 当我收到那个信号时?

3 个答案:

答案 0 :(得分:3)

我得到了我的问题的答案。我不知道它是否合适但通过这样做我的代码工作 我刚刚在欢迎区内移动onsigLogin 像这样的东西

Welcome{id:welcomePage
    width:parent.width
    height:parent.height
    visible:true
    onSigLogin: {
        visible=false
        login.visible=true
    }
} 

答案 1 :(得分:1)

不要调用其他对象,发出信号或绑定

当您忘记命令式代码时,像QML这样的声明性语言非常有用。将此模式用于您需要的任何元素:

ElementOne{
    id: first
    width:parent.width
    height:parent.height
    visible: !second.loggedIn // <- Beautiful binding
}
ElementTwo{
    id: second
    width:parent.width
    height:parent.height
    property bool loggedIn: false // Internal to the ElementTwo.qml
    visible: true
}

然后你的第二个元素可以改变它自己的loggedIn属性,欢迎可以自动做出反应。 这使得可重复使用的组件可以避免遍布整个地方的spagetti属性。 QML太危险了。

您可以使用其他模式,例如States,但主要是避免QML中的命令式代码。

答案 2 :(得分:1)

简单的答案是您使用Connections错误。来自the documentation

  

target:Object

     

此属性包含发送信号的对象。

     

如果未设置此属性,目标默认为。的父级   连接。

     

如果设置为null,则不进行任何连接,并且任何信号处理程序都是   忽略,直到目标不为空。

因此,在您的情况下,目标默认为main,因为它是Connections对象的父级。只需像这样修改代码:

Connections {
    target: welcomePage  // this is the critical part!
    ignoreUnknownSignals: true
    onSigLogin: {  // note capitalization of on*S*igLogin
        welcomePage.visible = false
        login.visible = true
    }
}

足以使其按预期工作,因为welcomePage是产生信号的那个。