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. [SOLVED]Cannot call c++ function from qml
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Cannot call c++ function from qml

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 3.4k Views 1 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.
  • K Offline
    K Offline
    khajvah
    wrote on last edited by
    #1

    @QGuiApplication app(argc, argv);

    QQmlEngine engine;
    
    
    QQmlContext* maincontext = new QQmlContext(engine.rootContext());
    
    MainWindowMC mainMC(maincontext);
    
    QQmlComponent component(&engine, QUrl::fromLocalFile("qml/convQML/main.qml"));
    
    
    QQuickWindow* window =  qobject_cast<QQuickWindow*> (component.create(maincontext&#41;);
    window->show();
    
    return app.exec&#40;&#41;;@
    

    This is my main.cpp.

    I pass pointer of context to MainWindowMC.
    Here is MainWindowMC:
    @MainWindowMC::MainWindowMC(QQmlContext* maincontext)
    :MainM()
    {

    typeGenerator();
    
    maincontext->setContextProperty("mainwin", this);
    
    maincontext->setContextProperty("typemodel", QVariant::fromValue(m_typemodel));
    
    maincontext->setContextProperty("unitmodel", QVariant::fromValue(m_unitmodel));
    

    }@

    and the class declaration:
    @public:

    MainWindowMC(QQmlContext*);
    
    QStringList typemodel();
    
    void typeGenerator();
    
    Q_INVOKABLE void unitGenerator(QString type);
    

    @

    It seems I have done everything - registered object in context, made Q_INVOKABLE function-but I cannot call the funtion from QML. It throws "TypeError: Object [object Object] has no method 'unitGenerator'"
    What am I doing wrong?

    Optionaly, this is how I try to use the function in QML:

    @ onCurrentTextChanged: mainwin.unitGenerator(typebox.currentText)
    @

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Wow, this is really strange way of instantiating QML engine and view. Why are you doing this in such a complicated manner?

      Is your MainWindowMC a QObject? Have you added the Q_OBJECT macro?

      (Z(:^

      1 Reply Last reply
      0
      • K Offline
        K Offline
        khajvah
        wrote on last edited by
        #3

        bq. Wow, this is really strange way of instantiating QML engine and view. Why are you doing this in such a complicated manner?Is your MainWindowMC a QObject? Have you added the Q_OBJECT macro?

        MainWindowMC is dreived from a class which on the other hand derives from QObject. I had a an issue with Q_OBJECT. The thing is mainM has Q_OBJECT macro (base class of MainWindowMC). -When I add Q_OBJECT in MainWindowMC, it throws some strange errors but I think Q_OBJECT moves to MainWindowMC ad it is derived from mainM- I have added Q_OBJECT macro but still the same problem.

        Which part is complicated? I have just started using QML, so I don't know much about it. I think everything is as it should be. I make engine, give the pointer of context to a class(I don't want to make my main.cpp too long) and I create component with that context?

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qnoobish
          wrote on last edited by
          #4

          I think first you have to load the QML file, then try to get the rootContext and set the property to use your class object with invokable functions.

          @
          QQmlEngine engine;
          QQmlComponent component(&engine, QUrl::fromLocalFile("qml/convQML/main.qml"));

          QQmlContext* maincontext = new QQmlContext(engine.rootContext());

          MainWindowMC mainMC(maincontext);
          @

          In short, software is eating the world.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Well for me the easy way is this, but everybody has their own:
            @
            // from memory, sorry for any errors
            QQuickView *view = new QQuickView();

            MainWindowMC *mainMC = new MainWindowMC();
            view->rootContext()->setCOntextProperty("myMain", mainMC);

            view->setSource(QUrl::fromUserInput("myQml.qml"));
            view->show();
            return app.exec();
            @

            (Z(:^

            1 Reply Last reply
            0
            • K Offline
              K Offline
              khajvah
              wrote on last edited by
              #6

              Anyways ppl, sorry for wasting your time, it is not my fault. I cleaned and rebuilt and it started working (probably some pre-compilation issues)

              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