QUdpSocket HeartBeat behaviour [SOLVED]
Hello Guys, I need to send the status of my Qt application through and Upd socket every seconds. How can I achive this? Should I subclass QThread e using QThread::sleep or is there a best approch ? thanks
Good idea to use QThread and sleep there in a while loop.
Or you can use QTimer to trigger some slot every N time value you want...
I like the QTimer solution. Any possible drawbacks?
You can use a QTimer inside a QThread loop like this :
connect(&timer, SIGNAL(timeout()), this, SLOT(sendStatu()));
Important : when using signal and slots inside the thread run() function you must add exec(); This is because they need an event loop to be executed!
Only one drawback: it works uses signal/slot connection, but it is also an advantage: No extra loops and triggered exactly when you need it... nothing more...
Ok but If a use the QTimer solutin could I avoid to subclass QThread?
You can create a normal QObject that does the timing and writing to the socket, then use "QObject::moveToThread()":http://qt-project.org/doc/qt-4.8/qobject.html#moveToThread to have it run in that thread.
A good example of how to do this is "this post":http://codethis.wordpress.com/2011/04/04/using-qthread-without-subclassing/
Acctually you shouldn't subclass QThread if possible. Have a look at "this wiki entry":http://qt-project.org/wiki/QThreads_general_usage for a best practise description concerning QThreads.
Another option is QRunnable but in your case its not really feasable since thats more for a one time fire and forget sort of approach.