Can't call my C++ function from QML



  • 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



  • @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);
    

    ?



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.