Unable to assign QJSValue to QColor



  • As stated here:

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

    and here:

    http://doc.qt.io/qt-5/qml-color.html

    QColor types should be automatically converted to color when passed to QML. Instead I receive the error:

    Unable to assign QJSValue to QColor

    Here the relevant declarations in my class:

    Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
    QColor color() { return m_color; }
    QColor m_color;
    

    On QML side:

    import QtQuick 2.0
    ListModel { id: myModel }
    function() {
        myModel.append( {
            "myColor": myClass.color
        } )
    }
    // in delegate
    Rectangle {
        width: 200
        height: 200
        color: myColor
    }
    

  • Moderators

    Hi @Mark81,
    As per the docs:

    Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).

    So IMO a QColor cannot be assigned to the role myColor. And this in turn causes problem in the delegate.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.