Important: Please read the Qt Code of Conduct -

Accessing Material colors in QML C++ extension

  • I have the following code in a C++ extension :

    class User : public QQuickItem
        Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
        Q_PROPERTY(int temperature READ temperature WRITE setTemperature NOTIFY temperatureChanged)
        Q_PROPERTY(QColor primaryColor READ primaryColor WRITE setPrimaryColor NOTIFY primaryColorChanged)
        QString m_name;
        QString m_userName;
        int m_temperature;
        QColor m_primaryColor= Material.color(Material.Indigo);

    How can i acess to the Material functions / Color enumerations?


    Rui Sebastiao

  • The enum is defined in a private header (qquickmaterialstyle_p.h) that isn't shipped with The Qt Company's binary packages. Without modifying a build, grabbing the enum at compile time purely from C++ doesn't appear to be possible.

    Some options are to parse the output of 'qmlplugindump -nonrelocatable QtQuick.Controls.Material 2.0', or use a piece of QML to store the enum values in properties of a QML object for retrieval in C++.

  • Assuming that User is registered as a QML type, the natural way would be to pass the color from QML:

    User {
        primaryColor: Material.color(Material.Indigo)

    This is essentially what the Material style itself does.

Log in to reply