'How can I connect a QML signal to a C++ slot within the QML file

according to this, I can connect qml signal and c++ slot in qml file without QObject::connect in c++ file.

But all I got is an error Expected token ':' in

Window {
    signal sizeChange(int y, int width, int height)
    visible: true
    width: 1920
    height: 1080

    sizeChange.connect(cefWindow.resizeCEFWindow)
                      ^
                      | Expected token ':'
}


Solution 1:[1]

There's a couple different ways you could do it. You could put your code in a function as @Amfasis mentioned:

Window {
    Component.onCompleted: {
        sizeChange.connect(cefWindow.resizeCEFWindow)
    }
}

Or you could just directly call your C++ slot from a signal handler, like this:

onSizeChange: {
    cefWindow.resizeCEFWindow()
}

I usually prefer the second method myself.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JarMan