Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. the program closes without error
Qt 6.11 is out! See what's new in the release blog

the program closes without error

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 544 Views 1 Watching
  • 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.
  • serkan_trS Offline
    serkan_trS Offline
    serkan_tr
    wrote on last edited by serkan_tr
    #1

    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"
            }
        }
    }
    
    
    Christian EhrlicherC serkan_trS 2 Replies Last reply
    0
    • serkan_trS serkan_tr

      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"
              }
          }
      }
      
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      serkan_trS 1 Reply Last reply
      1
      • serkan_trS serkan_tr

        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_trS Offline
        serkan_trS Offline
        serkan_tr
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @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.

          serkan_trS Offline
          serkan_trS Offline
          serkan_tr
          wrote on last edited by serkan_tr
          #4

          @Christian-Ehrlicher

            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)

          Christian EhrlicherC J.HilkJ 2 Replies Last reply
          0
          • serkan_trS serkan_tr

            @Christian-Ehrlicher

              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)

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            So you should make sure you get the correct instance instead a nullptr I would guess.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • serkan_trS serkan_tr

              @Christian-Ehrlicher

                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)

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @serkan_tr replace

              id: cpp_com
              

              with

              objectName: "cpp_com"
              

              and you might actually be able to find the child by object name :P

              also properly initialise your base QObject.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              serkan_trS 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @serkan_tr replace

                id: cpp_com
                

                with

                objectName: "cpp_com"
                

                and you might actually be able to find the child by object name :P

                also properly initialise your base QObject.

                serkan_trS Offline
                serkan_trS Offline
                serkan_tr
                wrote on last edited by
                #7

                @J-Hilk
                yes that worked, thank you

                1 Reply Last reply
                0
                • serkan_trS serkan_tr has marked this topic as solved on

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved