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

tests on QColor in qml



  • I have a property that is a QColor in my qt quick application. I want to use the lightness() method to test if the lightness is above or below a certain level. This method doesn't work in qml because it's a c++ class method. Can anyone recommend how to do this test in the qml section?

    Image {
            id: myImage
            anchors.verticalCenter: myBox.verticalCenter
            anchors.horizontalCenter: myBox.horizontalCenter
            height: .7 * myBox.height
            width: .7 * myBox.height
            sourceSize.height: 128
            sourceSize.width: 128
            fillMode: Image.PreserveAspectFit
            source: "someImage.png"
            visible: (myClass.someColor.lightnessF() < 10)        <--- this is the problem 
        }
    


  • http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

    [edit]

    "This method doesn't work in qml because it's a c++ class method."

    That's a false assertion. You can go QML to c++ and c++ to qml rather easily and it's probably the most documented area of the documentation.



  • @kgregory said in tests on QColor in qml:

    visible: (myClass.someColor.lightnessF() < 10) <--- this is the problem

    The QColor property you pass from C++ to QML gets automatically converted to a QML basic Color type which apparently does not have any properties other than its color value...
    http://doc.qt.io/qt-5/qml-color.html

    So, to get the result of lightnessF(), you will need to add your own method to your custom C++ class, e.g. like:

        Q_INVOKABLE qreal lightnessF(){return m_color.lightnessF();}
    


  • @6thC said in tests on QColor in qml:

    That's a false assertion

    It actually is not, in this case. QColor automatically gets converted to Color, and Color has no methods, it seems?. The lightnessF() method is therefore a pure C++ method, and cannot be accessed directly in QML by passing a QColor object.



  • I think I misunderstood. My comments were around the QML / Cpp integration and not on QColor - I just head you couldn't signal between qml and cpp ... nvm me, I didn't read it deeply enough it seems.

    Sadly, I've been bit like this too (I think) where I use QML charts but need cpp replace() ... it's a pity they aren't 100% feature identical but I guess it is what it is hey.


Log in to reply