Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DPlatformTheme::property not work #219

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/dnativesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ void DNativeSettings::init(const QMetaObject *metaObject)

QMetaPropertyBuilder op;

switch (mp.type()) {
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
switch (mp.metaType()) {
#else
switch (static_cast<int>(mp.type())) {
#endif
case QMetaType::QByteArray:
case QMetaType::QString:
case QMetaType::QColor:
Expand Down Expand Up @@ -411,7 +415,7 @@ int DNativeSettings::metaCall(QMetaObject::Call _c, int _id, void ** _a)
const int index = p.propertyIndex();
// 对于本地属性,此处应该从m_settings中读写
if (Q_LIKELY(index != m_flagPropertyIndex && index != m_allKeysPropertyIndex
&& index >= m_firstProperty)) {
&& index >= m_firstProperty + m_propertyCount)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该不会有这种低级错误才对,确定这里应该加上 m_propertyCount ?会不会导致原本应该从 m_settings 里读取的属性读不到了?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题说的就是调用属性的 read 方法 themeName 可以, 无法通过 property("themeName") 读取到正确的值。

QByteArray DPlatformTheme::themeName() const
{
//    FETCH_PROPERTY("Net/ThemeName", themeName)
    D_DC(DPlatformTheme);
    QVariant value = d->theme->getSetting(QByteArrayLiteral("Net/ThemeName"));
    if (d->fallbackProperty && !value.isValid() && d->parent)
        return d->parent->themeName();

    return value.toByteArray();
}

themeName() 会通过私有成员 d->theme ->getSetting 获取 xsettings 中的 Net/ThemeName,如果直接 property("themeName") 会到 xsettings 中去读 themeName 自然是读不到的无效值。

修改方案就是 如果是原来的属性就 fallback 到源对象的 read 方法,即 m_base->qt_metacall(_c, _id, _a);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class Q_GUI_EXPORT QGuiApplication : public QCoreApplication
{
    Q_OBJECT
    Q_PROPERTY(QIcon windowIcon READ windowIcon WRITE setWindowIcon)
    Q_PROPERTY(QString applicationDisplayName READ applicationDisplayName WRITE setApplicationDisplayName NOTIFY applicationDisplayNameChanged)
    Q_PROPERTY(QString desktopFileName READ desktopFileName WRITE setDesktopFileName)
    Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
    Q_PROPERTY(QString platformName READ platformName STORED false)
    Q_PROPERTY(bool quitOnLastWindowClosed  READ quitOnLastWindowClosed WRITE setQuitOnLastWindowClosed)
    Q_PROPERTY(QScreen *primaryScreen READ primaryScreen NOTIFY primaryScreenChanged STORED false)
}
    const QMetaObject *metaObj = qGuiApp->metaObject();
    QMetaObjectBuilder ob;
    ob.addMetaObject(metaObj);
    int offset = metaObj->propertyOffset();
    int count = ob.propertyCount(); // metaObj->propertyCount();包含了 parent 的 property
    for (int i = offset; i < count + offset; ++i) {
        qInfo() << "[" << i << "]" << metaObj->property(i).name();
    }
[ 6 ] windowIcon
[ 7 ] applicationDisplayName
[ 8 ] desktopFileName
[ 9 ] layoutDirection
[ 10 ] platformName
[ 11 ] quitOnLastWindowClosed
[ 12 ] primaryScreen

switch (_c) {
case QMetaObject::ReadProperty:
*reinterpret_cast<QVariant*>(_a[1]) = m_settings->setting(p.name());
Expand Down