Important: Please read the Qt Code of Conduct -

how to expose subclass methods to qml

  • Hi I have a main class which has instances of many subclasses. I would like to expose all methods of class and its instances of subclasess to QML.
    Similar Question like below but the answer received not working.

  • Qt Champions 2018

    Define "not working", that's not a good diagnosis.

    Show us your code.

  • @srikanth2006 said in how to expose subclass methods to qml:

    of many subclasses.

    Just in case, please note that the example for the link you posted has no "subclassing" action at all, i.e. SubClass is not a subclass of MainClass, they're both subclasses of QObject.

    class MainClass: QObject
       Subclass subclass;
    class Subclass:QObject
      public slots:
      void someMethod();

  • Hi ,
    Sorry I will reframe my question with some code below:
    So I have a mainWindowClass and DeviceManagerclass. In mainWindowclass I instantiate object of deviceManagerClass. like below:

    #include "devicemanagerdialog.h"
    class MainWindow : public QObject
    explicit MainWindow(QObject *parent = nullptr);
    //Some signals and some slots etc
    DeviceManagerDialog *deviceManagerDialogObj = new DeviceManagerDialog();

    IN QML main cpp
    #include <QQmlContext>
    #include "mainwindow.h"
    MainWindow mainWindow_C_QML;
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    So I expected in main.qml that deviceManagerDialogObj and itsmethods are exposed. However I can only mainWindowQML methods but not like mainWindowQML.deviceManagerDialogObj->...

    My question is how can I access deviceManagerDialogObj and all its methods instantied in mainWindow class . I hope it is clear? Thanks

  • @srikanth2006 said in how to expose subclass methods to qml:


    You won't access such component not in QML or even in C++ because:

    DeviceManagerDialog *deviceManagerDialogObj = new DeviceManagerDialog();

    you made it a private member of MainWindow!

    You will need to provide getter(s) for such element.

  • ah yes sorry that was public. It is not private. It is typo.
    I cant access them even when it is public. I hope it is possible?

Log in to reply