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. Data sent from CPP causes interface crash in QML
Forum Updated to NodeBB v4.3 + New Features

Data sent from CPP causes interface crash in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 491 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.
  • Y Offline
    Y Offline
    YigitCtnkaya
    wrote on last edited by SGaist
    #1

    Hello, first of all, I have a problem like this. I want to transfer the data to the QML side with a signal from C ++, but after a while my interface crashes without giving any error. I can't find the reason for this, I can't see it even though I debug, is there any error in my code?
    NOTE: I didn't add unnecessary parts of the code
    NOTE2: QT 5.15.16

    QML:
    Window {
     QtObject {
        id: tcpVariables
        property real tcp_velocity_X_Angle: 0
    }
       Connections {
                target: tcpClient
                 function onVelocity_X_Changed() {
                   var newVelocity = tcpClient.get_Velocity_X()
    
                   if (!isNaN(newVelocity) && isFinite(newVelocity)) {
    
                           tcpVariables.tcp_velocity_X_Angle = newVelocity;
                             console.log("tcp_velocity_X_Angle signal triggered: " + tcpVariables.tcp_velocity_X_Angle);
    
                   } else {
                        console.error("Invalid velocity value: " + newVelocity);
                   }
               }
            }
    }
    

    tcpClient.h:

    class TcpClient : public QObject
    {
        Q_OBJECT
    public:
        Q_PROPERTY(qreal float_X_velocity READ get_Velocity_X WRITE setVelocity_X NOTIFY velocity_X_Changed)
        QML_ELEMENT
    public:
        Q_INVOKABLE qreal get_Velocity_X() const {return float_X_velocity; }
         void  setVelocity_X (qreal value);
        qreal float_X_velocity; /**/
    
    signals:
        void velocity_X_Changed();
    

    tcpClient.cpp ( CODE USAGE ):

     qint32 x_velocity_int = (static_cast<qint32>(data[29]) << 24) |
                                                    (static_cast<qint32>(data[28]) << 16) |
                                                    (static_cast<qint32>(data[27]) << 8) |
                                                    (static_cast<qint32>(data[26]) & 0xff);
    float float_X_velocity;
    std::memcpy(&float_X_velocity, &x_velocity_int, sizeof(float));
       setVelocity_X(static_cast<qreal>(float_X_velocity));
    
    tcpClient.cpp: (FUNCTION)
    void TcpClient::setVelocity_X(qreal value) {
        if (!qFuzzyCompare(float_X_velocity, value)) {
            if (qIsNaN(value) || !qIsFinite(value)) {
                qWarning() << "Invalid velocity value: " << value;
                return; // Geçersiz değeri atama
            }
            float_X_velocity = value;
            emit velocity_X_Changed();
            qDebug() << "float_X_velocity signal emitted, new value: " << float_X_velocity;
        }
    }
    

    main.cpp:

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        // OpenGL USAGE
        QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
      // TCP OBJECTS
        TcpClient tcpclient;
    
        // UI OBJECTS
        QQmlApplicationEngine engine;
     engine.rootContext()->setContextProperty("tcpClient", &tcpclient);
      const QUrl url(QStringLiteral("../qml/Screens/splashScreen.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
             if (!obj && url == objUrl)
                 QCoreApplication::exit(-1);
         }, Qt::QueuedConnection);
    
        engine.load(url);
    
        videoStreamer.startCapture();
    
        return app.exec();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Do you have a stack trace of that crash ?
      Why are you using a memcpy to assign an qint32 to a float and then static cast it to qreal ? There's no need for all of that. Also what makes you think that the size of a float is the same or smaller as the size of a qint32 ?
      On a side note, please use coding tags next time you post code.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        YigitCtnkaya
        wrote on last edited by
        #3

        First of all, thank you,

        Next time I will be careful to use code blocks.

        What kind of correction do you suggest, I thought it would be correct this way?

        What do you mean by stack trace of that crash?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The correct way ? Just call setVelocity_X(x_velocity_int); directly.

          Well, if your application is crashing, run it with the debugger until it crashes and there you'll have the stack trace of the crash which is the position of the crash and the functions call stack that lead to it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Y 1 Reply Last reply
          0
          • SGaistS SGaist

            The correct way ? Just call setVelocity_X(x_velocity_int); directly.

            Well, if your application is crashing, run it with the debugger until it crashes and there you'll have the stack trace of the crash which is the position of the crash and the functions call stack that lead to it.

            Y Offline
            Y Offline
            YigitCtnkaya
            wrote on last edited by
            #5

            @SGaist
            I use it to correct the data, and if I do it the same way as you say, the result is the same.

            When I analyze it with the debugger, my interface crashes after about 20 seconds, so it's hard to follow.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Well, since it crashes quickly with the debugger, you should already have a stack trace at hand to share with us.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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