Don't use contextProperty to make KAboutData available on QML

Context properties always takes in a QVariant, which means that
whenever you access the property it is re-evaluated because in
between each access the property may be changed as
setContextProperty() can be used at any moment in time.
This commit is contained in:
Felipe Kinoshita 2021-09-28 17:46:01 -03:00
parent b4d67ffeb4
commit e1c80bff3d
No known key found for this signature in database
GPG Key ID: E5FF0D0C1A82B6A1
5 changed files with 38 additions and 3 deletions

View File

@ -3,6 +3,7 @@
set(SRCS_base
main.cpp
about.cpp
fetcher.cpp
database.cpp
entry.cpp

7
src/about.cpp Normal file
View File

@ -0,0 +1,7 @@
/**
* SPDX-FileCopyrightText: 2021 Felipe Kinoshita <kinofhek@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "about.h"

27
src/about.h Normal file
View File

@ -0,0 +1,27 @@
/**
* SPDX-FileCopyrightText: 2021 Felipe Kinoshita <kinofhek@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#pragma once
#include <QObject>
#include <KAboutData>
class AboutType : public QObject
{
Q_OBJECT
Q_PROPERTY(KAboutData aboutData READ aboutData CONSTANT)
public:
static AboutType &instance()
{
static AboutType _instance;
return _instance;
}
[[nodiscard]] KAboutData aboutData() const
{
return KAboutData::applicationData();
}
};

View File

@ -31,6 +31,7 @@
#ifdef Q_OS_ANDROID
#include "androidlogging.h"
#endif
#include "about.h"
#include "audiomanager.h"
#include "author.h"
#include "database.h"
@ -118,8 +119,6 @@ int main(int argc, char *argv[])
}
about.processCommandLine(&parser);
engine.rootContext()->setContextProperty(QStringLiteral("_aboutData"), QVariant::fromValue(about));
qmlRegisterType<FeedsProxyModel>("org.kde.kasts", 1, 0, "FeedsProxyModel");
qmlRegisterType<QueueModel>("org.kde.kasts", 1, 0, "QueueModel");
qmlRegisterType<EpisodeProxyModel>("org.kde.kasts", 1, 0, "EpisodeProxyModel");
@ -132,6 +131,7 @@ int main(int argc, char *argv[])
qmlRegisterUncreatableType<EpisodeModel>("org.kde.kasts", 1, 0, "EpisodeModel", QStringLiteral("Only for enums"));
qmlRegisterUncreatableType<FeedsModel>("org.kde.kasts", 1, 0, "FeedsModel", QStringLiteral("Only for enums"));
qmlRegisterSingletonInstance("org.kde.kasts", 1, 0, "AboutType", &AboutType::instance());
qmlRegisterSingletonInstance("org.kde.kasts", 1, 0, "Fetcher", &Fetcher::instance());
qmlRegisterSingletonInstance("org.kde.kasts", 1, 0, "Database", &Database::instance());
qmlRegisterSingletonInstance("org.kde.kasts", 1, 0, "DataManager", &DataManager::instance());

View File

@ -14,5 +14,5 @@ import org.kde.kasts 1.0
Kirigami.AboutPage {
title: i18n("About")
aboutData: _aboutData
aboutData: AboutType.aboutData
}