Kcm: How to register c++ plugin for qml while being able to access that object

I’m trying to create a kcm for grub2 with qml.
I have a KcmGrub2 Class and GrubData class. GrubData class contains the properties that will be accessed by qml.

I have tried the code below

QJSValue KcmGrub2::dataSingleton(QQmlEngine *engine, QJSEngine *scriptEngine)
{
    Q_UNUSED(engine)
    auto result = new GrubData();
    return scriptEngine->newQObject(result);
}

KcmGrub2::KcmGrub2(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
    : KQuickAddons::ManagedConfigModule(parent, metaData, args)
{
    qmlRegisterSingletonType("org.kde.kcms.grub2", 1, 0, "Data", dataSingleton);
}

But the problem here is that i believe i cannot access the GrubData object from KcmGrub2. Therefore it seems like i cannot change the state of apply button with the GrubData object.

So how do i register the object while being able to access it from the KcmGrub2 class?

Also how do i handle the state of apply button? point me to some code examples please. I did look through the code of some kcms but sadly i was unable to find it myself

There’s no need to manually create JavaScript objects (I don’t think that even works?) I recommend following the Qt QML C++ doc page which explains everything you need to know about registering singletons.

Also in your example, it looks like your passing the function itself into qmlRegisterSingleton :slight_smile: You only posted a small snippet though.

1 Like

Also there is a tutorial about how to write a KCM, that contains some helpful advices Settings module (KCM) development | Developer

As of what i read through the docs, i found that QQmlEngine Class | Qt QML 5.15.12 tells me i have to allocate the singleton object before the engine to make sure it outlives engine but in this scenario, I believe there is no way to allocate it before the engine. So how do i go around this issue. When i just tried that way without somehow allocating before engine, I get The registered singleton has already been deleted. Ensure that it outlives the engine.

Full source code kcm-grub/src at master · Thenujan-0/kcm-grub · GitHub
The code snipped i provided is from kcm-grub/kcmgrub2.cpp at master · Thenujan-0/kcm-grub · GitHub

I have no experiene with KCM development, but take a look through the Look and Feel KCM as it’s similar to your goal: a QML UI and a C++ model backing.

From what I understand:

  • Use KQuickAddons::ManagedConfigModule.
  • Don’t bother using a singleton, it’s not needed and it’s not what KCMs use anyway.
  • You can access the KCM object through org.kde.kcm. See the C++ Q_PROPERTY declared here and the QML access used here.
  • You can then handle creating QObjects on the C++ side, and then exposing them using Qt object properties like any other QML project.
1 Like

I did look through few kcms for anything like this, But didn’t check this one. Thank you so much :heart: