Home > OS >  How can I connect a QML signal to a C slot within the QML file
How can I connect a QML signal to a C slot within the QML file

Time:04-26

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 ':'
}

CodePudding user response:

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.

  • Related