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. Can't call my C++ function from QML
QtWS25 Last Chance

Can't call my C++ function from QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.7k Views
  • 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 Offline
    D Offline
    DavidM29
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • D DavidM29

      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

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @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
      5
      • 6thC6 Offline
        6thC6 Offline
        6thC
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DavidM29
          wrote on last edited by
          #4

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

          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