Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. how to expose subclass methods to qml
Forum Updated to NodeBB v4.3 + New Features

how to expose subclass methods to qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 471 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    srikanth2006
    wrote on last edited by srikanth2006
    #1

    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

    Pablo J. RoginaP 1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

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

      Show us your code.

      1 Reply Last reply
      1
      • S srikanth2006

        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

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @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();
        }
        

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • S Offline
          S Offline
          srikanth2006
          wrote on last edited by srikanth2006
          #4

          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

          Pablo J. RoginaP 1 Reply Last reply
          0
          • S srikanth2006

            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

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @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.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • S Offline
              S Offline
              srikanth2006
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved