How to get the Material Style primary color enum value



  • I'm cross-posting this from my stack overflow question here since it didn't gain much attention there, so some of you might have seen this question before - otherwise I'm hoping to get some fresh eyes on it here.

    Background:

    Quick Controls 2.1 has a Material Design style for its components.

    As part of Material design you pick a primary and accent colour as part of a colour scheme, this is done by choosing an enum value from this chart here and adding it to qtquickcontrols2.conf as follows:

    [Material]
    Theme=Light
    Accent=Red
    Primary=Blue
    

    In this example the primary colour is "Blue" which is from the enum Material.Blue.

    Material Design lets you shade a colour for use as a background etc by another enum seen in this chart here. You do this by calling the color(Material.Blue, Material.shade50) method (docs). The method signature is: color color(enumeration predefined, enumeration shade).

    Question:

    Since the color method requires the colour's enum value, how can you retreive, at runtime in qml, the enum value set via qtquickcontrols2.conf?

    The Material style's attached properties only seems to expose the primary and accent colours as a color qml type instead of it's enum value (docs here) and attempting to use this in a scenario such as Material.color(Material.primary, Material.shade50) yields the wrong colour.

    This means that any time a shade of the primary or accent colour is required a hardcoded enum value must be written into the qml files, instead of referencing what was set in qtquickcontrols2.conf.

    Does anyone know the correct way to retreive the original enum value?



  • I've been reading the source for the Material style which is located here and header here.

    In the header and source it seems that the primary colour enum value is held as uint by the field "m_primary", this value is never returned directly, instead only via line 519:

    QVariant QQuickMaterialStyle::primary() const
    {
        return primaryColor();
    }
    

    And primaryColor() translates this uint to an RGB value via a lookup table at line 778:

    QColor QQuickMaterialStyle::primaryColor() const
    {
        if (m_customPrimary)
            return QColor::fromRgba(m_primary);
        if (m_primary > BlueGrey)
            return QColor();
        return colors[m_primary][Shade500];
    }
    

    m_primary color is initiliased via a static called "globalPrimary" which is declared at line 376:

    static uint globalPrimary = QQuickMaterialStyle::Indigo;
    

    and initialised at line 1185 to the value in qtquickcontrols2.conf along with m_primary:

    QByteArray primaryValue = resolveSetting("QT_QUICK_CONTROLS_MATERIAL_PRIMARY", settings, QStringLiteral("Primary"));
            Color primaryEnum = toEnumValue<Color>(primaryValue, &ok);
            if (ok) {
                globalPrimaryCustom = m_customPrimary = false;
                globalPrimary = m_primary = primaryEnum;
            }
    

    Follow Up Question
    There is no accessor for the raw private field m_primary, nor for the static globalPrimary. Does anyone know of a hack to access either of those values from QML? It seems like an omission that there is no way to re-shade the primary and accent colour.

    Note:
    All code snippets above are Qt source code and their re-use is bound by Qt's license:

    /****************************************************************************
    **
    ** Copyright (C) 2017 The Qt Company Ltd.
    ** Contact: http://www.qt.io/licensing/
    **
    ** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:LGPL3$
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and The Qt Company. For licensing terms
    ** and conditions see http://www.qt.io/terms-conditions. For further
    ** information use the contact form at http://www.qt.io/contact-us.
    **
    ** GNU Lesser General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU Lesser
    ** General Public License version 3 as published by the Free Software
    ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
    ** packaging of this file. Please review the following information to
    ** ensure the GNU Lesser General Public License version 3 requirements
    ** will be met: https://www.gnu.org/licenses/lgpl.html.
    **
    ** GNU General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU
    ** General Public License version 2.0 or later as published by the Free
    ** Software Foundation and appearing in the file LICENSE.GPL included in
    ** the packaging of this file. Please review the following information to
    ** ensure the GNU General Public License version 2.0 requirements will be
    ** met: http://www.gnu.org/licenses/gpl-2.0.html.
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/
    

Log in to reply
 

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