Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Make Connections to Loader depending on loaded File



  • I load a loader object with a specific file like this:

    onClicked: {
                        questionSqlTableModel.generateNewRandomQuestions(2)
                        loader.setSource("quiz/Quiz.qml",
                                         {"randomQuestions":
                                             questionSqlTableModel.randomQuestions})
                    }
    

    Now the page Quiz.qml emits a signal I need to wait for. This I can check with a Connection:

    Connections {
           target: loader.item
    
           function onFinnished(correctAnswers) {
               console.log("correct Answer: " +  correctAnswers)
           }
       }
    

    Now my loader can also load other pages:

                ToolButton {
                    text: qsTr("Show Table")
                    icon.name: "document-open"
                    onClicked: {
                        loader.setSource("sql_table_view/SqlTableView.qml")
                    }
                }
    

    Do I have to dynamically enable disable the Connection depending on the content? If yes how can I only listen for the connection if a specific file is loaded?

    Like

    Load file A connect to Signal B emitted from A
    Load file C connect to Signal D emitted from C

    edit: do I have to set ignoreUnknownSignals to false when the page is not loaded from Connections or active = false ?

    edit:

    I tired to disable the Connection when loading another element like this:

    Connections {
        id: quizConnections
        target: loader.item
    
        function onFinnished(correctAnswers) {
            console.log("correct Answer: " +  correctAnswers)
            quizConnections.enabled = false
            loader.setSource("result/Result.qml")
        }
    }
    

    But then I get this error:

    QML Connections: Detected function "onFinnished" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.
    

  • Moderators

    @sandro4912 said in Make Connections to Loader depending on loaded File:

    do I have to set ignoreUnknownSignals to false when the page is not loaded from Connections or active = false ?

    use active property. You don't need to call it in the slot, you can do it declaratively instead:

    Connections {
        id: quizConnections
        target: loader.item
        active: loader.source === "quiz/Quiz.qml"
    
        function onFinnished(correctAnswers) {
            console.log("correct Answer: " +  correctAnswers)
        }
    }
    

    Or even set the target only when needed:

    Connections {
        id: quizConnections
        target: loader.source === "quiz/Quiz.qml"? loader.item : undefined
    
        function onFinnished(correctAnswers) {
            console.log("correct Answer: " +  correctAnswers)
        }
    }
    


  • @sierdzio said in Make Connections to Loader depending on loaded File:

    Connections {
    id: quizConnections
    target: loader.item
    active: loader.source === "quiz/Quiz.qml"

    function onFinnished(correctAnswers) {
        console.log("correct Answer: " +  correctAnswers)
    }
    

    }

    That gives error:

    Cannot assign to non-existent property "active"
    

    I tried instead:

    enabled: loader.source === "quiz/Quiz.qml"
    

    which gives still:

    qrc:/qml/main.qml:53:5: QML Connections: Detected function "onFinnished" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.
    

    I think to surpress the warning we have to set ignoreUnknownSignals to true aswell


  • Moderators

    @sandro4912 said in Make Connections to Loader depending on loaded File:

    I think to surpress the warning we have to set ignoreUnknownSignals to true aswell

    Yeah in that case it seems like a good choice.


Log in to reply