Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. tests on QColor in qml
Qt 6.11 is out! See what's new in the release blog

tests on QColor in qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kgregory
    wrote on last edited by
    #1

    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 
        }
    
    DiracsbracketD 1 Reply Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by 6thC
      #2

      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.

      DiracsbracketD 1 Reply Last reply
      0
      • K kgregory

        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 
            }
        
        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #3

        @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();}
        
        1 Reply Last reply
        2
        • 6thC6 6thC

          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.

          DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          2
          • 6thC6 Offline
            6thC6 Offline
            6thC
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved