Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Can't call my C++ function from QML

    QML and Qt Quick
    3
    4
    1453
    Loading More Posts
    • 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.
    • D
      DavidM29 last edited by

      Hello,

      I tried to make a C++ function that call the terminal via the system() method. But my problem is that I can't get it to work on my qml side. I have this error :

      TypeError: Property 'send' of object true is not a function
      

      I don't really understand I already have some C++ function in my QML.
      Here is the code :
      terminal.h :

      #ifndef TERMINAL_H
      #define TERMINAL_H
      
      #include <QObject>
      
      class Terminal : public QObject
      {
          Q_OBJECT
      public:
          Terminal(QObject *parent = NULL);
          Q_INVOKABLE void send(const char *commande);
      };
      
      #endif // TERMINAL_H
      
      

      terminal.cpp :

      #include "terminal.h"
      #include <stdlib.h>
      
      Terminal::Terminal(QObject *parent) :
          QObject(parent)
      {
      
      }
      
      void Terminal::send(const char *commande){
          system(commande);
      }
      
      

      main.cpp lines to expose this Class :

      
      
      #include <terminal.h>
      
      Terminal cons();
      
       view->rootContext()->setContextProperty("cons", &cons);
      
      

      I also have this warning :

      main.cpp:40: warning: the address of 'Terminal cons()' will always evaluate as 'true' [-Waddress]
           view->rootContext()->setContextProperty("cons", &cons);                                                          
      

      It is probably something really basic or simple but I don't see it...
      Thank you for your help in advance

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @DavidM29 last edited by JonB

        @DavidM29

        Terminal cons();

        [(I think) this declares cons as a function returning a Terminal.]

        Do you not mean either:

        Terminal cons;
        view->rootContext()->setContextProperty("cons", &cons);
        

        or

        Terminal *cons = new Terminal;
        view->rootContext()->setContextProperty("cons", cons);
        

        ?

        1 Reply Last reply Reply Quote 5
        • 6thC
          6thC last edited by

          Have you registered Terminal? http://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html
          probably want something like qmlRegisterUncreatableType()

          1 Reply Last reply Reply Quote 0
          • D
            DavidM29 last edited by

            Thank you for your answer !
            JonB you were wright ! this declared a function !
            It works fine now !

            1 Reply Last reply Reply Quote 0
            • First post
              Last post