the program closes without error
-
I am trying to communicate c++ with qml, but the program closes. the program is being built without any problems, but when I run it, it opens and closes.
#ifndef COMM_H #define COMM_H #include <QObject> #include <QDebug> #include <QTimer> #include <string> using namespace std; class SerialPort: public QObject{ Q_OBJECT private: QTimer* timer; int i; public: SerialPort(); ~SerialPort(); }; class CommQMl: public QObject{ Q_OBJECT Q_PROPERTY(string name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(int number_class READ number_class WRITE setNumber_class NOTIFY number_classChanged) private: string m_name; int m_number_class; public: CommQMl(); ~CommQMl(); string name() const; void setName(const string &newName); int number_class() const; void setnumber_class(int newNumber_class); signals: void nameChanged(); void number_classChanged(); }; #endif // COMM_H#include "comm.h" SerialPort::SerialPort():i(0){ qDebug() << "SerialPort class destroyed"; } SerialPort::~SerialPort(){ qDebug() << "SerialPort class created"; delete timer; } /////////////////////////////////////////////////////// CommQMl::CommQMl(): m_number_class(0) { qDebug() << "CommQML class created"; } CommQMl::~CommQMl() { qDebug() << "CommQML class destroyed"; } string CommQMl::name() const { return m_name; } void CommQMl::setName(const string &newName) { if (m_name == newName) return; m_name = newName; emit nameChanged(); } int CommQMl::number_class() const { return m_number_class; } void CommQMl::setnumber_class(int newNumber_class) { if (m_number_class == newNumber_class) return; m_number_class = newNumber_class; emit number_classChanged(); }main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "comm.h" int main(int argc, char *argv[]) { qmlRegisterType<CommQMl>("qmlcomm", 1, 0, "CommQMl"); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); QTimer timer; engine.load(url); int i = 0; QObject* rootObject = engine.rootObjects().first(); CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); QObject::connect(&timer, &QTimer::timeout, [&]() { myObject->setnumber_class(10); }); timer.start(1000); app.exec(); return 0 ; }main.qml
import QtQuick 2.15 import QtQuick.Window 2.15 import qmlcomm 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") CommQMl{ id:cpp_comm } Rectangle{ height: 100 width: 100 anchors.centerIn: parent color:"black" Text { id: aaa text: cpp_comm.number_class anchors.centerIn: parent color:"green" } } } -
I am trying to communicate c++ with qml, but the program closes. the program is being built without any problems, but when I run it, it opens and closes.
#ifndef COMM_H #define COMM_H #include <QObject> #include <QDebug> #include <QTimer> #include <string> using namespace std; class SerialPort: public QObject{ Q_OBJECT private: QTimer* timer; int i; public: SerialPort(); ~SerialPort(); }; class CommQMl: public QObject{ Q_OBJECT Q_PROPERTY(string name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(int number_class READ number_class WRITE setNumber_class NOTIFY number_classChanged) private: string m_name; int m_number_class; public: CommQMl(); ~CommQMl(); string name() const; void setName(const string &newName); int number_class() const; void setnumber_class(int newNumber_class); signals: void nameChanged(); void number_classChanged(); }; #endif // COMM_H#include "comm.h" SerialPort::SerialPort():i(0){ qDebug() << "SerialPort class destroyed"; } SerialPort::~SerialPort(){ qDebug() << "SerialPort class created"; delete timer; } /////////////////////////////////////////////////////// CommQMl::CommQMl(): m_number_class(0) { qDebug() << "CommQML class created"; } CommQMl::~CommQMl() { qDebug() << "CommQML class destroyed"; } string CommQMl::name() const { return m_name; } void CommQMl::setName(const string &newName) { if (m_name == newName) return; m_name = newName; emit nameChanged(); } int CommQMl::number_class() const { return m_number_class; } void CommQMl::setnumber_class(int newNumber_class) { if (m_number_class == newNumber_class) return; m_number_class = newNumber_class; emit number_classChanged(); }main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "comm.h" int main(int argc, char *argv[]) { qmlRegisterType<CommQMl>("qmlcomm", 1, 0, "CommQMl"); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); QTimer timer; engine.load(url); int i = 0; QObject* rootObject = engine.rootObjects().first(); CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); QObject::connect(&timer, &QTimer::timeout, [&]() { myObject->setnumber_class(10); }); timer.start(1000); app.exec(); return 0 ; }main.qml
import QtQuick 2.15 import QtQuick.Window 2.15 import qmlcomm 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") CommQMl{ id:cpp_comm } Rectangle{ height: 100 width: 100 anchors.centerIn: parent color:"black" Text { id: aaa text: cpp_comm.number_class anchors.centerIn: parent color:"green" } } }@serkan_tr said in the program closes without error:
CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm");
You should check if this returns a valid pointer.
-
I am trying to communicate c++ with qml, but the program closes. the program is being built without any problems, but when I run it, it opens and closes.
#ifndef COMM_H #define COMM_H #include <QObject> #include <QDebug> #include <QTimer> #include <string> using namespace std; class SerialPort: public QObject{ Q_OBJECT private: QTimer* timer; int i; public: SerialPort(); ~SerialPort(); }; class CommQMl: public QObject{ Q_OBJECT Q_PROPERTY(string name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(int number_class READ number_class WRITE setNumber_class NOTIFY number_classChanged) private: string m_name; int m_number_class; public: CommQMl(); ~CommQMl(); string name() const; void setName(const string &newName); int number_class() const; void setnumber_class(int newNumber_class); signals: void nameChanged(); void number_classChanged(); }; #endif // COMM_H#include "comm.h" SerialPort::SerialPort():i(0){ qDebug() << "SerialPort class destroyed"; } SerialPort::~SerialPort(){ qDebug() << "SerialPort class created"; delete timer; } /////////////////////////////////////////////////////// CommQMl::CommQMl(): m_number_class(0) { qDebug() << "CommQML class created"; } CommQMl::~CommQMl() { qDebug() << "CommQML class destroyed"; } string CommQMl::name() const { return m_name; } void CommQMl::setName(const string &newName) { if (m_name == newName) return; m_name = newName; emit nameChanged(); } int CommQMl::number_class() const { return m_number_class; } void CommQMl::setnumber_class(int newNumber_class) { if (m_number_class == newNumber_class) return; m_number_class = newNumber_class; emit number_classChanged(); }main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "comm.h" int main(int argc, char *argv[]) { qmlRegisterType<CommQMl>("qmlcomm", 1, 0, "CommQMl"); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); QTimer timer; engine.load(url); int i = 0; QObject* rootObject = engine.rootObjects().first(); CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); QObject::connect(&timer, &QTimer::timeout, [&]() { myObject->setnumber_class(10); }); timer.start(1000); app.exec(); return 0 ; }main.qml
import QtQuick 2.15 import QtQuick.Window 2.15 import qmlcomm 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") CommQMl{ id:cpp_comm } Rectangle{ height: 100 width: 100 anchors.centerIn: parent color:"black" Text { id: aaa text: cpp_comm.number_class anchors.centerIn: parent color:"green" } } }@serkan_tr I noticed that the problem is that the myObject object is not found by the program, but I don't understand why it can't find it
-
@serkan_tr said in the program closes without error:
CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm");
You should check if this returns a valid pointer.
QObject* rootObject = engine.rootObjects().first(); CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); if(myObject){ qDebug("found"); }in this code if() command returns 0
CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); qDebug() << myObject;output: QObject(0x0)
-
QObject* rootObject = engine.rootObjects().first(); CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); if(myObject){ qDebug("found"); }in this code if() command returns 0
CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); qDebug() << myObject;output: QObject(0x0)
So you should make sure you get the correct instance instead a nullptr I would guess.
-
QObject* rootObject = engine.rootObjects().first(); CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); if(myObject){ qDebug("found"); }in this code if() command returns 0
CommQMl* myObject = rootObject->findChild<CommQMl*>("cpp_comm"); qDebug() << myObject;output: QObject(0x0)
@serkan_tr replace
id: cpp_comwith
objectName: "cpp_com"and you might actually be able to find the child by object name :P
also properly initialise your base QObject.
-
@serkan_tr replace
id: cpp_comwith
objectName: "cpp_com"and you might actually be able to find the child by object name :P
also properly initialise your base QObject.
-
S serkan_tr has marked this topic as solved on