Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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.
    https://stackoverflow.com/questions/44056476/accessing-instances-of-a-class-from-qml-context


  • 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
    {
       Q_OBJECT
       Subclass subclass;
       MainClass();
    
    }
    
    class Subclass:QObject
    {
      Q_Object
      Subclass();
    
      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
    {
    Q_OBJECT
    public:
    explicit MainWindow(QObject *parent = nullptr);
    ~MainWindow();
    //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;
    engine.rootContext()->setContextProperty("mainWindowQML",&mainWindow_C_QML);
    

    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:

    mainWindowQML.deviceManagerDialogObj

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

    ...
    private:
    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