Let Controls inherit palettes and fonts from parents
The Item's palette property and Control's font property returns the resolved value; its resolveMask is inherited from parent object, when the return value is used in the qml. For example: ApplicationWindow { id: window palette { window: "blue"; windowText: "blue" } Control { id: control } Button { onClicked: { control.palette.window = "red" window.palette.windowText = "red" console.log(control.palette.windowText) } } } In Button.onClicked, `control.palette` is the `resolvedPalette` of Control, it's resolveMask is not 0. Next, the new value is assigned to the control, then the control's "requestedPalette" will change to the new value, and it's resolveMask also is not 0. Next, `window.palette.windowText = "red"` changes the `windowText` palette for the window; QQuickPaletteColorProvider::inheritPalette() will be called. Because the resolveMask of `requestedPalette` is equal to the new palette, the control will not inherit any colors from its parent. So `console.log(control.palette.windowText)` was printing `qml: #0000ff`, but the right result is `qml: #ff0000`. When PaletteProvider is missing the inherited struct, fallbackPalette is now used as parentPalette. [ChangeLog][Controls] Controls now inherit palette and font from parents. Done-With: Volker Hilsheimer <volker.hilsheimer@qt.io> Done-With: yeshanshan <yeshanshan@uniontech.com> Done-With: Shawn Rutledge <shawn.rutledge@qt.io> Pick-to: 6.4 Fixes: QTBUG-101480 Change-Id: Ibe400f647512331352bf34dee42f908fb7a8d914 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io> Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
This commit is contained in:
parent
8310c3cba7
commit
2483ffa011
|
@ -23,7 +23,7 @@ class QQuickAbstractPaletteProvider
|
|||
public:
|
||||
virtual ~QQuickAbstractPaletteProvider() = default;
|
||||
virtual QPalette defaultPalette() const = 0;
|
||||
virtual QPalette parentPalette() const = 0;
|
||||
virtual QPalette parentPalette(const QPalette &fallbackPalette) const { return fallbackPalette; }
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
|
|
@ -20,7 +20,6 @@ class DefaultPalettesProvider : public QQuickAbstractPaletteProvider
|
|||
{
|
||||
public:
|
||||
QPalette defaultPalette() const override { static QPalette p; return p; }
|
||||
QPalette parentPalette() const override { return defaultPalette(); }
|
||||
};
|
||||
|
||||
static std::default_delete<const QQuickAbstractPaletteProvider> defaultDeleter() { return {}; }
|
||||
|
@ -37,10 +36,10 @@ const QColor &QQuickPaletteColorProvider::color(QPalette::ColorGroup group, QPal
|
|||
|
||||
bool QQuickPaletteColorProvider::setColor(QPalette::ColorGroup g, QPalette::ColorRole r, QColor c)
|
||||
{
|
||||
m_requestedPalette.value() = m_resolvedPalette;
|
||||
ensureRequestedPalette();
|
||||
m_requestedPalette->setColor(g, r, c);
|
||||
|
||||
return inheritPalette(paletteProvider()->parentPalette());
|
||||
return updateInheritedPalette();
|
||||
}
|
||||
|
||||
bool QQuickPaletteColorProvider::resetColor(QPalette::ColorGroup group, QPalette::ColorRole role)
|
||||
|
@ -54,7 +53,7 @@ bool QQuickPaletteColorProvider::resetColor(QPalette::ColorGroup group, QPalette
|
|||
bool QQuickPaletteColorProvider::fromQPalette(QPalette p)
|
||||
{
|
||||
m_requestedPalette.value() = std::move(p);
|
||||
return inheritPalette(paletteProvider()->parentPalette());
|
||||
return updateInheritedPalette();
|
||||
}
|
||||
|
||||
QPalette QQuickPaletteColorProvider::palette() const
|
||||
|
@ -77,7 +76,7 @@ void QQuickPaletteColorProvider::setPaletteProvider(const QQuickAbstractPaletteP
|
|||
bool QQuickPaletteColorProvider::copyColorGroup(QPalette::ColorGroup cg,
|
||||
const QQuickPaletteColorProvider &p)
|
||||
{
|
||||
m_requestedPalette.value() = m_resolvedPalette;
|
||||
ensureRequestedPalette();
|
||||
|
||||
auto srcPalette = p.palette();
|
||||
for (int roleIndex = QPalette::WindowText; roleIndex < QPalette::NColorRoles; ++roleIndex) {
|
||||
|
@ -87,7 +86,7 @@ bool QQuickPaletteColorProvider::copyColorGroup(QPalette::ColorGroup cg,
|
|||
}
|
||||
}
|
||||
|
||||
return inheritPalette(paletteProvider()->parentPalette());
|
||||
return updateInheritedPalette();
|
||||
}
|
||||
|
||||
bool QQuickPaletteColorProvider::reset()
|
||||
|
@ -95,21 +94,59 @@ bool QQuickPaletteColorProvider::reset()
|
|||
return fromQPalette(QPalette());
|
||||
}
|
||||
|
||||
bool QQuickPaletteColorProvider::inheritPalette(const QPalette &p)
|
||||
/*! \internal
|
||||
Merge the given \a palette with the existing requested palette, remember
|
||||
that it is the inherited palette (in case updateInheritedPalette() is
|
||||
called later), and update the stored palette (to be returned from
|
||||
\l palette()) if the result is different. Returns whether the stored
|
||||
palette got changed.
|
||||
*/
|
||||
bool QQuickPaletteColorProvider::inheritPalette(const QPalette &palette)
|
||||
{
|
||||
auto inheritedMask = m_requestedPalette.isAllocated() ? m_requestedPalette->resolveMask() | p.resolveMask() : p.resolveMask();
|
||||
QPalette parentPalette = m_requestedPalette.isAllocated() ? m_requestedPalette->resolve(p) : p;
|
||||
m_lastInheritedPalette.value() = palette;
|
||||
return doInheritPalette(palette);
|
||||
}
|
||||
|
||||
/*! \internal
|
||||
Merge the given \a palette with the existing requested palette, and update
|
||||
the stored palette (to be returned from \l palette()) if the result is
|
||||
different. Returns whether the stored palette got changed.
|
||||
*/
|
||||
bool QQuickPaletteColorProvider::doInheritPalette(const QPalette &palette)
|
||||
{
|
||||
auto inheritedMask = m_requestedPalette.isAllocated() ? m_requestedPalette->resolveMask() | palette.resolveMask()
|
||||
: palette.resolveMask();
|
||||
QPalette parentPalette = m_requestedPalette.isAllocated() ? m_requestedPalette->resolve(palette) : palette;
|
||||
parentPalette.setResolveMask(inheritedMask);
|
||||
|
||||
auto tmpResolvedPalette = parentPalette.resolve(paletteProvider()->defaultPalette());
|
||||
tmpResolvedPalette.setResolveMask(tmpResolvedPalette.resolveMask() | inheritedMask);
|
||||
|
||||
bool changed = notEq(tmpResolvedPalette, m_resolvedPalette);
|
||||
if (changed) {
|
||||
if (changed)
|
||||
std::swap(tmpResolvedPalette, m_resolvedPalette);
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
/*! \internal
|
||||
Update the stored palette (to be returned from \l palette()) from the
|
||||
parent palette. Returns whether the stored palette got changed.
|
||||
*/
|
||||
bool QQuickPaletteColorProvider::updateInheritedPalette()
|
||||
{
|
||||
// Use last inherited palette as parentPalette's fallbackPalette: it's useful when parentPalette doesn't exist.
|
||||
const QPalette &p = m_lastInheritedPalette.isAllocated() ? m_lastInheritedPalette.value()
|
||||
: paletteProvider()->defaultPalette();
|
||||
return doInheritPalette(paletteProvider()->parentPalette(p));
|
||||
}
|
||||
|
||||
void QQuickPaletteColorProvider::ensureRequestedPalette()
|
||||
{
|
||||
if (m_requestedPalette.isAllocated())
|
||||
return;
|
||||
|
||||
m_requestedPalette.value() = QPalette();
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
|
|
@ -45,11 +45,16 @@ public:
|
|||
|
||||
bool reset();
|
||||
|
||||
bool inheritPalette(const QPalette &p);
|
||||
bool inheritPalette(const QPalette &palette);
|
||||
|
||||
private:
|
||||
bool doInheritPalette(const QPalette &palette);
|
||||
bool updateInheritedPalette();
|
||||
void ensureRequestedPalette();
|
||||
|
||||
QPalette m_resolvedPalette;
|
||||
QLazilyAllocated<QPalette> m_requestedPalette;
|
||||
QLazilyAllocated<QPalette> m_lastInheritedPalette;
|
||||
|
||||
using Deleter = std::function<void(const QQuickAbstractPaletteProvider*)>;
|
||||
using ProviderPtr = std::unique_ptr<const QQuickAbstractPaletteProvider, Deleter>;
|
||||
|
|
|
@ -87,16 +87,16 @@ public:
|
|||
/*!
|
||||
\internal
|
||||
|
||||
A default palette for this component.
|
||||
The default palette for this component.
|
||||
*/
|
||||
QPalette defaultPalette() const override;
|
||||
|
||||
/*!
|
||||
\internal
|
||||
|
||||
A parent palette for this component. Can be null.
|
||||
The parent palette for this component. Can be null.
|
||||
*/
|
||||
QPalette parentPalette() const override;
|
||||
QPalette parentPalette(const QPalette &fallbackPalette) const override;
|
||||
|
||||
/*!
|
||||
\internal
|
||||
|
@ -214,7 +214,7 @@ void QQuickPaletteProviderPrivateBase<I, Impl>::registerPalette(PalettePtr palet
|
|||
|
||||
m_palette = std::move(palette);
|
||||
m_palette->setPaletteProvider(this);
|
||||
m_palette->inheritPalette(parentPalette());
|
||||
m_palette->inheritPalette(parentPalette(defaultPalette()));
|
||||
|
||||
setCurrentColorGroup();
|
||||
|
||||
|
@ -254,7 +254,7 @@ QQuickPalette *QQuickPaletteProviderPrivateBase<I, Impl>::windowPalette() const
|
|||
}
|
||||
|
||||
template<class I, class Impl>
|
||||
QPalette QQuickPaletteProviderPrivateBase<I, Impl>::parentPalette() const
|
||||
QPalette QQuickPaletteProviderPrivateBase<I, Impl>::parentPalette(const QPalette &fallbackPalette) const
|
||||
{
|
||||
if constexpr (!isRootWindow<I>()) {
|
||||
for (auto parentItem = itemWithPalette()->parentItem(); parentItem;
|
||||
|
@ -271,7 +271,7 @@ QPalette QQuickPaletteProviderPrivateBase<I, Impl>::parentPalette() const
|
|||
}
|
||||
}
|
||||
|
||||
return defaultPalette();
|
||||
return fallbackPalette;
|
||||
}
|
||||
|
||||
template<class I>
|
||||
|
@ -343,8 +343,8 @@ void QQuickPaletteProviderPrivateBase<I, Impl>::connectItem()
|
|||
|
||||
if constexpr (!isRootWindow<I>()) {
|
||||
// Item with palette has the same lifetime as its implementation that inherits this class
|
||||
I::connect(itemWithPalette(), &I::parentChanged , [this]() { inheritPalette(parentPalette()); });
|
||||
I::connect(itemWithPalette(), &I::windowChanged , [this]() { inheritPalette(parentPalette()); });
|
||||
I::connect(itemWithPalette(), &I::parentChanged , [this]() { inheritPalette(parentPalette(defaultPalette())); });
|
||||
I::connect(itemWithPalette(), &I::windowChanged , [this]() { inheritPalette(parentPalette(defaultPalette())); });
|
||||
I::connect(itemWithPalette(), &I::enabledChanged, [this]() { setCurrentColorGroup(); });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,6 @@ T.ComboBox {
|
|||
validator: control.validator
|
||||
selectByMouse: control.selectTextByMouse
|
||||
|
||||
font: control.font
|
||||
color: control.editable ? control.palette.text : control.palette.buttonText
|
||||
selectionColor: control.palette.highlight
|
||||
selectedTextColor: control.palette.highlightedText
|
||||
|
|
|
@ -52,7 +52,6 @@ T.ComboBox {
|
|||
validator: control.validator
|
||||
selectByMouse: control.selectTextByMouse
|
||||
|
||||
font: control.font
|
||||
color: control.editable ? control.palette.text : control.palette.buttonText
|
||||
selectionColor: control.palette.highlight
|
||||
selectedTextColor: control.palette.highlightedText
|
||||
|
|
|
@ -66,7 +66,6 @@ T.ComboBox {
|
|||
validator: control.validator
|
||||
selectByMouse: control.selectTextByMouse
|
||||
|
||||
font: control.font
|
||||
color: control.flat ? control.palette.windowText : control.editable ? control.palette.text : control.palette.buttonText
|
||||
selectionColor: control.palette.highlight
|
||||
selectedTextColor: control.palette.highlightedText
|
||||
|
|
|
@ -29,7 +29,6 @@ T.GroupBox {
|
|||
bottomPadding: background.bottomPadding
|
||||
|
||||
text: control.title
|
||||
font: control.font
|
||||
elide: Text.ElideRight
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
|
||||
|
|
|
@ -22,7 +22,6 @@ NativeStyle.DefaultComboBox {
|
|||
validator: control.validator
|
||||
selectByMouse: control.selectTextByMouse
|
||||
|
||||
font: control.font
|
||||
color: control.editable ? control.palette.text : control.palette.buttonText
|
||||
selectionColor: control.palette.highlight
|
||||
selectedTextColor: control.palette.highlightedText
|
||||
|
|
|
@ -57,7 +57,6 @@ T.ComboBox {
|
|||
validator: control.validator
|
||||
selectByMouse: control.selectTextByMouse
|
||||
|
||||
font: control.font
|
||||
color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
|
||||
selectionColor: control.Material.accentColor
|
||||
selectedTextColor: control.Material.primaryHighlightedTextColor
|
||||
|
|
|
@ -62,7 +62,6 @@ T.ComboBox {
|
|||
validator: control.validator
|
||||
selectByMouse: control.selectTextByMouse
|
||||
|
||||
font: control.font
|
||||
color: !control.enabled ? control.Universal.chromeDisabledLowColor :
|
||||
control.editable && control.activeFocus ? control.Universal.chromeBlackHighColor : control.Universal.foreground
|
||||
selectionColor: control.Universal.accent
|
||||
|
|
|
@ -37,7 +37,6 @@ T.ComboBox {
|
|||
validator: control.validator
|
||||
selectByMouse: control.selectTextByMouse
|
||||
|
||||
font: control.font
|
||||
color: control.editable ? control.palette.text : control.palette.buttonText
|
||||
selectionColor: control.palette.highlight
|
||||
selectedTextColor: control.palette.highlightedText
|
||||
|
|
|
@ -500,7 +500,7 @@ QQuickAccessibleAttached *QQuickControlPrivate::accessibleAttached(const QObject
|
|||
/*!
|
||||
\internal
|
||||
|
||||
Returns the font that the control w inherits from its ancestors and
|
||||
Returns the font that the control \a item inherits from its ancestors and
|
||||
QGuiApplication::font.
|
||||
*/
|
||||
QFont QQuickControlPrivate::parentFont(const QQuickItem *item)
|
||||
|
@ -508,13 +508,13 @@ QFont QQuickControlPrivate::parentFont(const QQuickItem *item)
|
|||
QQuickItem *p = item->parentItem();
|
||||
while (p) {
|
||||
if (QQuickControl *control = qobject_cast<QQuickControl *>(p))
|
||||
return control->font();
|
||||
return QQuickControlPrivate::get(control)->resolvedFont;
|
||||
else if (QQuickLabel *label = qobject_cast<QQuickLabel *>(p))
|
||||
return label->font();
|
||||
return label->QQuickText::font();
|
||||
else if (QQuickTextField *textField = qobject_cast<QQuickTextField *>(p))
|
||||
return textField->font();
|
||||
return textField->QQuickTextInput::font();
|
||||
else if (QQuickTextArea *textArea = qobject_cast<QQuickTextArea *>(p))
|
||||
return textArea->font();
|
||||
return textArea->QQuickTextEdit::font();
|
||||
|
||||
p = p->parentItem();
|
||||
}
|
||||
|
@ -951,7 +951,10 @@ void QQuickControl::itemChange(QQuickItem::ItemChange change, const QQuickItem::
|
|||
QFont QQuickControl::font() const
|
||||
{
|
||||
Q_D(const QQuickControl);
|
||||
return d->resolvedFont;
|
||||
QFont font = d->resolvedFont;
|
||||
// The resolveMask should inherit from the requestedFont
|
||||
font.setResolveMask(d->extra.value().requestedFont.resolveMask());
|
||||
return font;
|
||||
}
|
||||
|
||||
void QQuickControl::setFont(const QFont &font)
|
||||
|
@ -1552,7 +1555,6 @@ void QQuickControl::setBackground(QQuickItem *background)
|
|||
text: qsTr("Button")
|
||||
contentItem: Label {
|
||||
text: control.text
|
||||
font: control.font
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
}
|
||||
}
|
||||
|
|
|
@ -287,7 +287,11 @@ QQuickLabel::~QQuickLabel()
|
|||
|
||||
QFont QQuickLabel::font() const
|
||||
{
|
||||
return QQuickText::font();
|
||||
Q_D(const QQuickLabel);
|
||||
QFont font = QQuickText::font();
|
||||
// The resolve mask should inherit from the requestedFont
|
||||
font.setResolveMask(d->extra.value().requestedFont.resolveMask());
|
||||
return font;
|
||||
}
|
||||
|
||||
void QQuickLabel::setFont(const QFont &font)
|
||||
|
|
|
@ -138,9 +138,9 @@ bool QQuickPopupItemPrivate::providesPalette() const
|
|||
return QQuickPopupPrivate::get(popup)->providesPalette();
|
||||
}
|
||||
|
||||
QPalette QQuickPopupItemPrivate::parentPalette() const
|
||||
QPalette QQuickPopupItemPrivate::parentPalette(const QPalette &fallbackPalette) const
|
||||
{
|
||||
return QQuickPopupPrivate::get(popup)->parentPalette();
|
||||
return QQuickPopupPrivate::get(popup)->parentPalette(fallbackPalette);
|
||||
}
|
||||
|
||||
void QQuickPopupItem::updatePolish()
|
||||
|
|
|
@ -101,7 +101,7 @@ public:
|
|||
QPalette defaultPalette() const override;
|
||||
bool providesPalette() const override;
|
||||
|
||||
QPalette parentPalette() const override;
|
||||
QPalette parentPalette(const QPalette &fallbackPalette) const override;
|
||||
|
||||
int backId = 0;
|
||||
int escapeId = 0;
|
||||
|
|
|
@ -526,7 +526,11 @@ QQuickTextAreaAttached *QQuickTextArea::qmlAttachedProperties(QObject *object)
|
|||
|
||||
QFont QQuickTextArea::font() const
|
||||
{
|
||||
return QQuickTextEdit::font();
|
||||
Q_D(const QQuickTextArea);
|
||||
QFont font = QQuickTextEdit::font();
|
||||
// The resolve mask should inherit from the requestedFont
|
||||
font.setResolveMask(d->extra.value().requestedFont.resolveMask());
|
||||
return font;
|
||||
}
|
||||
|
||||
void QQuickTextArea::setFont(const QFont &font)
|
||||
|
|
|
@ -387,7 +387,11 @@ QQuickTextField::~QQuickTextField()
|
|||
|
||||
QFont QQuickTextField::font() const
|
||||
{
|
||||
return QQuickTextInput::font();
|
||||
Q_D(const QQuickTextField);
|
||||
QFont font = QQuickTextInput::font();
|
||||
// The resolve mask should inherit from the requestedFont
|
||||
font.setResolveMask(d->extra.value().requestedFont.resolveMask());
|
||||
return font;
|
||||
}
|
||||
|
||||
void QQuickTextField::setFont(const QFont &font)
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 yeshanshan <yeshanshan@uniontech.com>.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** 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 https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** BSD License Usage
|
||||
** Alternatively, you may use this file under the terms of the BSD license
|
||||
** as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
|
||||
ApplicationWindow {
|
||||
id: window
|
||||
property alias control1: control1
|
||||
property alias control2: control2
|
||||
property alias control2ChildControl: control3
|
||||
property alias control4: control4
|
||||
|
||||
font {family: "Helvetica"; pointSize: 10; weight: 10}
|
||||
|
||||
Control {
|
||||
id: control1
|
||||
}
|
||||
|
||||
function changeFont1(font) {
|
||||
control1.font.pointSize = font.pointSize
|
||||
window.font.weight = font.weight
|
||||
}
|
||||
|
||||
Control {
|
||||
id: control2
|
||||
|
||||
Control {
|
||||
id: control3
|
||||
// The font's resolve mask of control4 should be is 0, the
|
||||
// controls3's font is same thing. because control3's font
|
||||
// resolve mask is 0 also, so the font's all value equal to
|
||||
// the parent font values.
|
||||
font: control4.font
|
||||
}
|
||||
}
|
||||
|
||||
Control {
|
||||
font.pointSize: 10
|
||||
|
||||
Control {
|
||||
id: control4
|
||||
}
|
||||
}
|
||||
|
||||
function changeFont2(font) {
|
||||
control2.font.pointSize = font.pointSize
|
||||
control2.font.weight = font.weight
|
||||
}
|
||||
}
|
|
@ -36,6 +36,8 @@ private slots:
|
|||
|
||||
void listView_data();
|
||||
void listView();
|
||||
|
||||
void resolve();
|
||||
};
|
||||
|
||||
static QFont testFont()
|
||||
|
@ -331,6 +333,44 @@ void tst_font::listView()
|
|||
QCOMPARE(control->property("font").value<QFont>().pixelSize(), 55);
|
||||
}
|
||||
|
||||
void tst_font::resolve()
|
||||
{
|
||||
QQmlEngine engine;
|
||||
QQmlComponent component(&engine);
|
||||
component.loadUrl(testFileUrl("resolve.qml"));
|
||||
|
||||
QScopedPointer<QObject> window(component.create());
|
||||
QVERIFY2(!window.isNull(), qPrintable(component.errorString()));
|
||||
|
||||
auto control1 = window->property("control1").value<QQuickControl*>();
|
||||
QVERIFY(control1);
|
||||
|
||||
QCOMPARE(window->property("font").value<QFont>().pointSize(),
|
||||
control1->property("font").value<QFont>().pointSize());
|
||||
QCOMPARE(window->property("font").value<QFont>().weight(),
|
||||
control1->property("font").value<QFont>().weight());
|
||||
QMetaObject::invokeMethod(window.get(), "changeFont1", Q_ARG(QVariant, QFont("Helvetica", 12, 12)));
|
||||
QVERIFY(window->property("font").value<QFont>().pointSize()
|
||||
!= control1->property("font").value<QFont>().pointSize());
|
||||
QCOMPARE(window->property("font").value<QFont>().weight(),
|
||||
control1->property("font").value<QFont>().weight());
|
||||
|
||||
QMetaObject::invokeMethod(window.get(), "changeFont2", Q_ARG(QVariant, QFont("Helvetica", 20, 20)));
|
||||
auto control2 = window->property("control2").value<QQuickControl*>();
|
||||
auto control2ChildControl = window->property("control2ChildControl").value<QQuickControl*>();
|
||||
auto control4 = window->property("control4").value<QQuickControl*>();
|
||||
QVERIFY(control2);
|
||||
QVERIFY(control2ChildControl);
|
||||
QVERIFY(control4);
|
||||
auto control2Font = control2->property("font").value<QFont>();
|
||||
auto control2ChildControlFont = control2ChildControl->property("font").value<QFont>();
|
||||
auto control4Font = control4->property("font").value<QFont>();
|
||||
QCOMPARE((int)control4Font.resolveMask(), 0);
|
||||
QCOMPARE(control4Font.resolveMask(), control2ChildControlFont.resolveMask());
|
||||
QCOMPARE(control2ChildControlFont, control2Font);
|
||||
QVERIFY(control2ChildControlFont != control4Font);
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_font)
|
||||
|
||||
#include "tst_font.moc"
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 zccrs <zccrs@live.com>, JiDe Zhang <zhangjide@uniontech.com>.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** 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 https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** BSD License Usage
|
||||
** Alternatively, you may use this file under the terms of the BSD license
|
||||
** as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
|
||||
ApplicationWindow {
|
||||
id: window
|
||||
property alias control: control
|
||||
|
||||
palette {window: "blue"; windowText: "white"}
|
||||
|
||||
Control {
|
||||
id: control
|
||||
}
|
||||
|
||||
function changeColors(color) {
|
||||
control.palette.window = color
|
||||
window.palette.windowText = color
|
||||
}
|
||||
}
|
|
@ -50,6 +50,8 @@ private slots:
|
|||
void setDynamicallyCreatedPalette();
|
||||
void createBindings();
|
||||
void updateBindings();
|
||||
|
||||
void resolve();
|
||||
};
|
||||
|
||||
tst_palette::tst_palette()
|
||||
|
@ -402,6 +404,29 @@ void tst_palette::updateBindings()
|
|||
QCOMPARE(QQuickItemPrivate::get(enabledButton)->palette()->button(), QColor("navy"));
|
||||
}
|
||||
|
||||
void tst_palette::resolve()
|
||||
{
|
||||
QQmlEngine engine;
|
||||
QQmlComponent component(&engine);
|
||||
component.loadUrl(testFileUrl("resolve.qml"));
|
||||
|
||||
QScopedPointer<QObject> window(component.create());
|
||||
QVERIFY2(!window.isNull(), qPrintable(component.errorString()));
|
||||
|
||||
auto control = window->property("control").value<QQuickControl*>();
|
||||
QVERIFY(control);
|
||||
|
||||
QCOMPARE(window->property("palette").value<QQuickPalette*>()->window(),
|
||||
control->property("palette").value<QQuickPalette*>()->window());
|
||||
QCOMPARE(window->property("palette").value<QQuickPalette*>()->windowText(),
|
||||
control->property("palette").value<QQuickPalette*>()->windowText());
|
||||
QMetaObject::invokeMethod(window.get(), "changeColors", Q_ARG(QVariant, QColor(Qt::red)));
|
||||
QVERIFY(window->property("palette").value<QQuickPalette*>()->window()
|
||||
!= control->property("palette").value<QQuickPalette*>()->window());
|
||||
QCOMPARE(window->property("palette").value<QQuickPalette*>()->windowText(),
|
||||
control->property("palette").value<QQuickPalette*>()->windowText());
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_palette)
|
||||
|
||||
#include "tst_palette.moc"
|
||||
|
|
|
@ -3,51 +3,89 @@
|
|||
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
import QtQuick.Templates as T
|
||||
import QtQuick.Dialogs
|
||||
import QtQuick.Layouts
|
||||
|
||||
ApplicationWindow {
|
||||
id: window
|
||||
visible: true
|
||||
width: 480
|
||||
width: 640
|
||||
height: 640
|
||||
title: qsTr("Hello World")
|
||||
title: qsTr("Font and Color Inheritance Test")
|
||||
font.pointSize: pointSizeSlider.value
|
||||
font.family: fontDialog.selectedFont.family
|
||||
palette.text: textColorDialog.selectedColor
|
||||
palette.buttonText: buttonTextColorDialog.selectedColor
|
||||
SystemPalette { id: systemPalette }
|
||||
|
||||
header: ToolBar {
|
||||
Slider {
|
||||
from: 16
|
||||
to: 48
|
||||
stepSize: 1
|
||||
onValueChanged: control.font.pointSize = value
|
||||
RowLayout {
|
||||
width: parent.width
|
||||
Slider {
|
||||
id: pointSizeSlider
|
||||
from: 6
|
||||
to: 48
|
||||
value: 12
|
||||
stepSize: 1
|
||||
}
|
||||
Label {
|
||||
text: pointSizeSlider.value + " pt " + font.family
|
||||
}
|
||||
Button {
|
||||
text: "Font…"
|
||||
palette.buttonText: systemPalette.buttonText
|
||||
onClicked: fontDialog.open()
|
||||
FontDialog { id: fontDialog }
|
||||
Component.onCompleted: fontDialog.selectedFont = window.font
|
||||
}
|
||||
Item { Layout.fillWidth: true }
|
||||
Button {
|
||||
text: "Text…"
|
||||
palette.buttonText: textColorDialog.selectedColor
|
||||
onClicked: textColorDialog.open()
|
||||
ColorDialog { id: textColorDialog }
|
||||
Component.onCompleted: textColorDialog.selectedColor = systemPalette.text
|
||||
Layout.margins: 3
|
||||
Layout.alignment: Qt.AlignRight
|
||||
}
|
||||
Button {
|
||||
text: "Buttons…"
|
||||
onClicked: buttonTextColorDialog.open()
|
||||
ColorDialog { id: buttonTextColorDialog }
|
||||
Component.onCompleted: buttonTextColorDialog.selectedColor = systemPalette.buttonText
|
||||
Layout.margins: 3
|
||||
Layout.alignment: Qt.AlignRight
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Flickable {
|
||||
anchors.fill: parent
|
||||
contentWidth: control.width
|
||||
contentHeight: control.height
|
||||
contentWidth: layout.implicitWidth + 40
|
||||
contentHeight: layout.implicitHeight + 40
|
||||
|
||||
T.Control {
|
||||
id: control
|
||||
width: layout.implicitWidth + 40
|
||||
height: layout.implicitHeight + 40
|
||||
ColumnLayout {
|
||||
id: layout
|
||||
anchors.fill: parent
|
||||
anchors.margins: 20
|
||||
Button { text: "Button" }
|
||||
CheckBox { text: "CheckBox" }
|
||||
GroupBox { title: "GroupBox" }
|
||||
RadioButton { text: "RadioButton" }
|
||||
Switch { text: "Switch" }
|
||||
TabButton {
|
||||
text: "TabButton"
|
||||
font.pointSize: control.font.pointSize
|
||||
}
|
||||
TextField { placeholderText: "TextField" }
|
||||
TextArea { placeholderText: "TextArea" }
|
||||
ToolButton { text: "ToolButton" }
|
||||
Tumbler { model: 3 }
|
||||
ColumnLayout {
|
||||
id: layout
|
||||
anchors.fill: parent
|
||||
anchors.margins: 20
|
||||
Label {
|
||||
text: "Label with **Bold** *Italics* _Underline_ ~~Strikethrough~~ `Mono`"
|
||||
textFormat: Label.MarkdownText
|
||||
}
|
||||
Button { text: "Button" }
|
||||
GroupBox {
|
||||
title: "GroupBox"
|
||||
ColumnLayout {
|
||||
RadioButton { text: "RadioButton" }
|
||||
CheckBox { text: "CheckBox" }
|
||||
}
|
||||
}
|
||||
Switch { text: "Switch" }
|
||||
TabButton { text: "TabButton" }
|
||||
TextField { placeholderText: "TextField" }
|
||||
TextArea { placeholderText: "TextArea" }
|
||||
ToolButton { text: "ToolButton" }
|
||||
Tumbler { model: 3 }
|
||||
}
|
||||
|
||||
ScrollBar.vertical: ScrollBar { }
|
||||
|
|
Loading…
Reference in New Issue