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 functionI 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_Hterminal.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 -
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 functionI 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_Hterminal.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 advanceTerminal cons();[(I think) this declares
consas a function returning aTerminal.]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()