Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connecting to the default propertyChanged signal



  • I have a property called brightness and want to connect the brightnessChanged signal to a C++ slot. Documentation says that you can write an on<Property>Changed signal handler, but does not explicitly say that a propertyChanged signal exists.

    If I try to add a signal brightnessChanged(int b) I get the error:

    Duplicate signal name: invalid override of property change signal or superclass signal
    

    which suggests that the propertyChanged signal exists. But if I try to connect to the brightnessChanged signal without declaring it in QML, I get

    QObject::connect: No such signal MyProject_QMLTYPE_11::brightnessChanged(int)
    

    So how can one connect to the default propertyChanged signal which apparently exists but somehow is not accessible? My current workaround is to have an onBrightnessChanged handler in QML which calls another brChanged signal (which then gets connected to the C++ slot), but that just seems sad.


  • Qt Champions 2017

    Did you define the brightnessChanged() in c++ or qml file ? From which component it is coming ?



  • brightness is a property in a QML file, and I want to connect the corresponding "Changed" signal to a C++ slot. I'm wondering if there is a way to reference this signal without explicitly defining it. (Note that there is no way to explicitly define brightnessChanged anyway, since that results in the Duplicate signal name error).



  • The issue is that you've specified the wrong signature for the signal: The propertyChanged signals from QML don't have an argument, so you would need to connect to brightnessChanged() instead of brightnessChanged(int). In that case you would need to read the property value afterwards though.

    However, IMHO the cleaner solution is to us a onBrithnessChanged handler in QML instead, and to call the C++ slot from there.



  • @FKosmale Ah, that explains it, thanks. The solution you propose is what I have implemented right now anyway; I had hoped there might be a more elegant way to do it. Thank you for clarifying.


Log in to reply