背景
Quick Controls 2.1的组件具有Material Design样式。
作为材质设计的一部分,您可以选择主色和强调色作为配色方案的一部分,方法是选择此chart here的枚举值并将其添加到qtquickcontrols2.conf
,如下所示:< / p>
[Material]
Theme=Light
Accent=Red
Primary=Blue
在此示例中,主要颜色为“蓝色”,来自枚举Material.Blue
。
“材质设计”允许您使用此chart here中显示的另一个枚举来着色颜色以用作背景等。您可以通过调用color(Material.Blue, Material.shade50)
方法(docs)来执行此操作。方法签名为:color color(enumeration predefined, enumeration shade)
。
问题:
由于color
方法需要颜色的枚举值,如何在运行时在qml中通过qtquickcontrols2.conf
检索枚举值?
Material
样式的附加属性似乎只将主色和强调色显示为color
qml类型而不是它的枚举值(docs here)并尝试在场景中使用它例如Material.color(Material.primary, Material.shade50)
会产生错误的颜色。
这意味着每当需要主色或强调色的阴影时,必须将硬编码的枚举值写入qml文件,而不是引用qtquickcontrols2.conf
中设置的内容
有谁知道检索原始枚举值的正确方法?
修改
我已在Qt论坛上发布此内容,链接为:here。
我添加了一些我将要粘贴的其他研究。
我一直在阅读材质样式的来源,该样式位于here和标题here。
在标题和来源中,主要颜色枚举值似乎由字段uint
保持为m_primary
,此值永远不会直接返回,而只能通过第519行返回:
QVariant QQuickMaterialStyle::primary() const
{
return primaryColor();
}
primaryColor()
通过第778行的查找表将此uint
转换为RGB
值:
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
颜色通过名为globalPrimary
的静态初始化,该静态在第376行声明:
static uint globalPrimary = QQuickMaterialStyle::Indigo;
并在第1185行初始化为qtquickcontrols2.conf
中的值以及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;
}
跟进问题:
原始私有字段m_primary和静态globalPrimary都没有访问者。有没有人知道从QML访问这些值中的任何一个?这似乎是一种遗漏,没有办法重新遮盖主色和强调色。
似乎我可能只是制作一个QML Singleton来保存这些颜色枚举值,然后引用单例而不是硬编码值。
注意: 上面的所有代码片段都是Qt源代码,它们的重复使用受Qt许可证的约束:
/****************************************************************************
**
** 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$
**
****************************************************************************/