QUdpSocket receive Datagram into QtConcurrent
-
@LudoFR
Well, firstly looks like VS & Qt are similar, which is good news. Secondly surprised it seems receiver is receiving more than sent? I don't think each side agrees on exactly what 1 second is :D@JonB said in QUdpSocket receive Datagram into QtConcurrent:
Secondly surprised it seems receiver is receiving more than sent?
@JonB Thanks god I'm not the only one...
@JonB said in QUdpSocket receive Datagram into QtConcurrent:
I don't think each side agrees on exactly what 1 second is :D
Science....is not an exact science... did performance computer impact that kind of information? Sender is an Asus Rog Strix3 with a config lower than my PC
-
Hello everyone,
Still learning on Qt, and I try to run a QUdpSocket receiving UDP data into a QtConcurrent thread, for avoiding GUI freezing.
Here my main.cpp
If i call "testComThread()" my function work and I receive my data, but if i call "QtConcurrent::run(testComThread);" I dont see any data into my console and my breakpoint in my "while" is never reached.
Did I need to call a kind of dispatcher or something maybe? This is my first use of QtConcurrent. Thanks in advance.
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <Manage/variablesglobales.h> #include <Manage/communications.h> #include <QtConcurrent> void testComThread(); int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif 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); engine.load(url); QFuture<void> test1 = QtConcurrent::run(testComThread); //testComThread(); return app.exec(); } void testComThread() { Communications* com = new Communications(); com->InitialiseConnection(); }my communication.cpp
#include "communications.h" Communications::Communications(QObject *parent) : QObject(parent) { } void Communications::InitialiseConnection() { udpSock = new QUdpSocket(this); udpSock->bind(QHostAddress::Any,50003); connect(udpSock,&QUdpSocket::readyRead,this,&Communications::ReceiveUdpPackage); } void Communications::ReceiveUdpPackage() { QHostAddress sender; quint16 port; while (udpSock->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSock->pendingDatagramSize()); udpSock->readDatagram(datagram.data(),datagram.size(),&sender,&port); qDebug() <<"Message From :" << datagram.data(); } }Thank you in advance.
@LudoFR said in QUdpSocket receive Datagram into QtConcurrent:
Hello everyone,
Still learning on Qt, and I try to run a QUdpSocket receiving UDP data into a QtConcurrent thread, for avoiding GUI freezing.
Here my main.cpp
If i call "testComThread()" my function work and I receive my data, but if i call "QtConcurrent::run(testComThread);" I dont see any data into my console and my breakpoint in my "while" is never reached.
Did I need to call a kind of dispatcher or something maybe? This is my first use of QtConcurrent. Thanks in advance.
Truncated code:
int main(int argc, char *argv[]) { QFuture<void> test1 = QtConcurrent::run(testComThread); return app.exec(); } void testComThread() { Communications* com = new Communications(); com->InitialiseConnection(); udpSock = new QUdpSocket(com); udpSock->bind(QHostAddress::Any,50003); connect(udpSock,&QUdpSocket::readyRead,this,&Communications::ReceiveUdpPackage); }This won't work. QtConcurrent::run() runs a function in a thread that doesn't have an event loop, and may cease to exist when the invoked function returns.
Regarding QML: The language has fairly strong rules regarding capitalization. Types should begin with an upper case letter. Properties and functions should begin with a lower case letter. Following the convention in C++ will make integration easier.
-
@JonB said in QUdpSocket receive Datagram into QtConcurrent:
Secondly surprised it seems receiver is receiving more than sent?
@JonB Thanks god I'm not the only one...
@JonB said in QUdpSocket receive Datagram into QtConcurrent:
I don't think each side agrees on exactly what 1 second is :D
Science....is not an exact science... did performance computer impact that kind of information? Sender is an Asus Rog Strix3 with a config lower than my PC
-
@LudoFR said in QUdpSocket receive Datagram into QtConcurrent:
Hello everyone,
Still learning on Qt, and I try to run a QUdpSocket receiving UDP data into a QtConcurrent thread, for avoiding GUI freezing.
Here my main.cpp
If i call "testComThread()" my function work and I receive my data, but if i call "QtConcurrent::run(testComThread);" I dont see any data into my console and my breakpoint in my "while" is never reached.
Did I need to call a kind of dispatcher or something maybe? This is my first use of QtConcurrent. Thanks in advance.
Truncated code:
int main(int argc, char *argv[]) { QFuture<void> test1 = QtConcurrent::run(testComThread); return app.exec(); } void testComThread() { Communications* com = new Communications(); com->InitialiseConnection(); udpSock = new QUdpSocket(com); udpSock->bind(QHostAddress::Any,50003); connect(udpSock,&QUdpSocket::readyRead,this,&Communications::ReceiveUdpPackage); }This won't work. QtConcurrent::run() runs a function in a thread that doesn't have an event loop, and may cease to exist when the invoked function returns.
Regarding QML: The language has fairly strong rules regarding capitalization. Types should begin with an upper case letter. Properties and functions should begin with a lower case letter. Following the convention in C++ will make integration easier.
@jeremy_k said in QUdpSocket receive Datagram into QtConcurrent:
Regarding QML: The language has fairly strong rules regarding capitalization. Types should begin with an upper case letter. Properties and functions should begin with a lower case letter. Following the convention in C++ will make integration easier.
Agreed and thanks, but i need to make a choice for my compagny and our futur tools, C++, python, Qt, PyCharm, VS.. first I check the language, the possibilities, time to develop with, the compatibily with all the dependency we need...
After our choice, I will check about the code convention, and use it in the final project, hope you have remarked this is not a real project !
But it's clearly not my priority now....