我想制作类似 Material.accent 的东西,我可以在其中更改父级和子级以获取父属性定义。
这是我此时所做的方式,但我在文档中找不到任何有关它的信息。我知道这是可能的,Material Style 也使用这种方法和其他东西,比如字体属性。
class MyThemeAttached : public QObject {
Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
QML_ANONYMOUS
public:
explicit MyThemeAttached(QObject* parent = nullptr)
: QObject(parent)
, m_color("#FF0000"){}
QColor color() const;
void setColor(const QColor color);
signals:
void backgroundChanged(QColor background);
private:
QColor m_color;
};
class MyTheme : public QObject
{
Q_OBJECT
QML_ATTACHED(MyThemeAttached)
QML_ELEMENT
public:
explicit MyTheme(QObject *parent = nullptr);
static MyThemeAttached *qmlAttachedProperties(QObject *object) {
return new MyThemeAttached(object);
}
};
ApplicationWindow {
id: root
visible: true
width: 800
height: 600
title: qsTr("Window")
MyCustomProperty.color: "orange"
Rectangle {
color: MyCustomProperty.color
}
}