Trigger an item's signal from outside javascript function



  • Hello Qt forum. I couldnt find the answer for my problem through searching. So im posting my first question here.
    I'm wondering if I could trigger an item's signal from a javascript function that lives outside the item (but in the same parent item aka qml-file).
    I'm trying to illustrate my problem with a generic example below. It's stripped to the things related to my question and not working as such but should give the idea.

    RowLayout {
        function fillInExistingData() {
    
            for (var i = 0; i < queryModelPtr.fieldCount(); i++) {
                var fieldData = queryModelPtr.getFieldData(i)
                var fieldName = fieldData[0].toLowerCase()+fieldData[1]
                try  { // try catch block necessary that loop doesn't stop on unknown fieldName
                    eval(fieldName).fieldText = fieldData[2]  // not recommended, but only way I know of
                } catch (exception) { console.log( "ERROR: " + fieldName + " doesn't exist")}
            }
        }
        ScrollView {
    
            TextArea {
                id: textInput
                onEditingFinished: dataEditFinished(inputTable, inputDataset,text)
                onTextChanged: dataChanged(inputTable, inputDataset,text)
                text: fieldText
            }
            ComboBox {
                id: comboInput
                (...)
                onAccepted: {
                    dataChanged(inputTable, inputDataset, index)
                }
                onActivated: {
                    dataEditFinished(inputTable, inputDataset,index)
                }
                Component.onCompleted: dataEditFinished(inputTable, inputDataset,comboInput.currentIndex)
            }
    
        }
    }
    

    I'm having a form with TextAreas and ComboBoxes. Users can fill in their information or can copy existing information from the data base with a copy button. The function fillInExistingData then populates my ComboBoxes and TextAreas. Every time that an item's data is changed, the input is stored in a temporary QHash in my C++-Object derived from QSqlQueryModel.
    Now, this design has the drawback that my dataChanged() signal is emitted on every character that my users are typing in. Much leaner would be using the editingFinished() signal. But this signal will not be emitted when data is filled in by the javascript function. I also cannot call my dataEditFinished Function directly from the Javascript function, because Comboboxes will return the currentIndex and TextAreas will return the text.

    So I would liketo trigger the editingFinished Signal from Javascript after filling in data in a TextArea or ComboBox. Something like this:

        function fillInExistingData() {
    
            for (var i = 0; i < queryModelPtr.fieldCount(); i++) {
               (...)
                try  {
                    eval(fieldName).fieldText = fieldData[2]  
                   eval(fieldName).editingFinished()
                } catch (exception) { console.log( "ERROR: " + fieldName + " doesn't exist")}
            }
        }
    
    

    But this doesn't work. Any Ideas how I could trigger this signal?



  • You can do that. Just see the example below. Your requirement is very similar to this. May be you have problem because editFinished() is given by the TextEdit itself. So you need to catch it and send your own signals. Following example may help u.

    import QtQuick 2.8
    import QtQuick.Window 2.2

    Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    function sendSignal() {
        top.sendMe();
    }
    
    Rectangle {
        id  : top
        signal sendMe();
        anchors.fill: parent
        anchors.margins: -10
        color: "transparent"
        border.width: 1
    }
    
    Connections {
        target: top
        onSendMe:{
            console.log("pthinks.com")
        }
    }
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            sendSignal()
        }
    }
    

    }



  • Thank you for your help dheerendra. Going through my code again, it turns out that I was calling my TextArea's parent component instead of the TextArea itself. Solved it now with a function inside my component that is then in turn calling the TextArea's signal. Will mark it as solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.