Search code examples
qtqmlsignals-slots

QML unexpected token '>' for slot connection with argument


I am working on a project on which I have a GUI with Qt/QML (5.12 in LGPL version for now on Linux) and the backend in C++. For now I am just working on the GUI part, with a QML component B1 which emit a signal I have initialised like this:

signal changeMode(string mode)

I connect this signal to a slot with another component B2 through the above component A which instanciated these B1 and B2 components. I do the connection with:

onChangeMode: (mode)=> B2.updateMode(mode)

But doing this, even if the compilation is okay and it is working when I execute the GUI, I have the following error message about the slot connection:

Unexpected token '>'

How can I fix this error message which is not really an error ?

For information, I am using the same syntax for signal/slot without argument and it is working. But, if I replace this syntax with the following one for the slot connection, the signal/slot mechanism is not working at all because I am not going in the function:

function onChangeMode(mode)
{
    B2.updateMode(mode)
}

Thank you in advance for your help or advice.


Solution

  • Update: This is probably caused by an outdated QtCreator version QTCREATORBUG-21757. It should be fixed from QtCreator 4.9 onwards.

    Keep in mind that there are other bugs related to the arrow function in QtCreator QTCREATORBUG-26305, QTCREATORBUG-23019, QTCREATORBUG-25198.

    Have a look on all the related issues: Qt Arrow Function


    The Arrow function expressions is available since Qt 5.12. In Qt6 there is also another syntax using function(mode). Have a look at this Jira comment.

    I think in your situation you could rely on the "magical" parameter injection. Just use the parameters defined in your signal directly in the bound expression. I know this isn't really readable and creates questions about where those "magical" parameters are coming from, but this is how it was done in early Qt5 versions.

    If you have defined your signal like this

    signal changeMode(string mode)
    

    your expression in the binding to the signal handler should look like this

    onChangeMode: { B2.updateMode(mode) }