Data sent from CPP causes interface crash in QML
-
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.16QML: 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(); }
-
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. -
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?
-
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.
-
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.
@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.
-
Well, since it crashes quickly with the debugger, you should already have a stack trace at hand to share with us.