0

main.qml

import QtQuick 2.10

Rectangle {
    id: rectangle
    signal searchTextEditedSignal(string text)
    SearchBox {
        id: search1
        textInput.onTextEditedSignal: searchTextEditedSignal(text)
    }
}

SearchBox.qml

import QtQuick 2.10

FocusScope {
    id: focusScope
    width: 250; height: 28

    TextInput {
        id: textInput
        signal textEditedSignal(string msg)
        selectByMouse: true
        onTextEdited: {
            textEditedSignal(textInput.text)
        }
    }
}

Then I got error at runtime:

qrc:/main.qml:15:9: Cannot assign to non-existent property "textInput"

I want to invoke the searchTextEditedSignal signal and use it in C++ code: connected to a C++ function. But the real invoker is the TextInput inside SearchBox.qml, so I need to delegate the textEditedSignal from SearchBox.qml to main.qml.

zwcloud
  • 4,546
  • 3
  • 40
  • 69
  • The id has a limited scope to the .qml file so you cannot and should not access an object using the id from another qml. In your particular case, define the textEditedSignal signal is focusScope: `FocusScope { id: focusScope width: 250; height: 28 signal textEditedSignal(string msg) TextInput { id: textInput selectByMouse: true onTextEdited: { focusScope.textEditedSignal(textInput.text) } } }` – eyllanesc Jul 31 '19 at 09:28
  • And then you can use it in main.qml: `SearchBox { id: search1 onTextEditedSignal: searchTextEditedSignal(text) }` – eyllanesc Jul 31 '19 at 09:29
  • 1
    Another method (but bad design probably) could be to alias the child: `FocusScope { ... property alias textInput: textInput ... }` – Amfasis Jul 31 '19 at 09:30
  • @Amfasis Exactly, it is a bad design. It is best to only expose the signals and properties through the root. – eyllanesc Jul 31 '19 at 09:32
  • 1
    I goes for eyllanesc's approach and it works! Only the object's signal should be exposed instead of the whole object, which ensures better encapsulation. – zwcloud Jul 31 '19 at 09:45

0 Answers0