Solved How to share one signal for two slots.
-
Thank you.
QObject::connect(&w.wifi_udp, &MyUDP::GetUdpMessage, &m_msgparser, &MSGPARSER::GetUdpMessage);
works good. -
@jenya7 said in How to share one signal for two slots.:
Thank you.
QObject::connect(&w.wifi_udp, &MyUDP::GetUdpMessage, &m_msgparser, &MSGPARSER::GetUdpMessage);
works good.I hope you have understand why and what you have do wrong.
-
I'd like to understand the whole concept.
My algorithm- Get a UDP message.
- Show a UDP message. (udp signal - main window slot)
Parse a UDP message. (udp signal - parser slot) - Put a parsed message in an array (parser signal - sensor class slot)
Everything connected in signal - slot paradigm?
-
@jenya7 said in How to share one signal for two slots.:
I'd like to understand the whole concept.
My algorithm- Get a UDP message.
- Show a UDP message. (udp signal - main window slot)
Parse a UDP message. (udp signal - parser slot)
3.Put a parsed message in an array (parser signal - sensor class slot)
Everything connected in signal - slot paradigm?
Sorry but I don't understand what about your question is?
It is the signals/slots mechanism? Then you should read Qt documentationIt made no sense to me to paraphrase it.
If you have question after reading it, then I could try to help you to understand some details.
But explaining the hole concept here is too large for a post... -
@KroMignon said in How to share one signal for two slots.:
@jenya7 said in How to share one signal for two slots.:
I'd like to understand the whole concept.
My algorithm- Get a UDP message.
- Show a UDP message. (udp signal - main window slot)
Parse a UDP message. (udp signal - parser slot)
3.Put a parsed message in an array (parser signal - sensor class slot)
Everything connected in signal - slot paradigm?
Sorry but I don't understand what about your question is?
It is the signals/slots mechanism? Then you should read Qt documentationIt made no sense to me to paraphrase it.
If you have question after reading it, then I could try to help you to understand some details.
But explaining the hole concept here is too large for a post...I'm afraid in a complex application I can not pull off everything with a signal-slot mechanism.
It's an application that should run on RaspberryPi. Usually (in C compilers) I did it likevoid main(void) { //setup is here while (1) { //all stuff is here } }
So in Qt I'd do like this
int main(int argc, char *argv[]) { QThread main_thread; // = new QThread; worker *m_worker = new worker(); m_worker->moveToThread(&main_thread); QObject::connect(&main_thread, &QThread::started, m_worker, &worker::Process); } //in worker.cpp __attribute__ ((noreturn)) void worker::Process() { while (1) { ok = GetUdpMessage(); if (ok ) ParseUdpMessage); ProcessMessages(); RunScript); } }
Just guessing.
-
@jenya7 said in How to share one signal for two slots.:
I'm afraid in a complex application I can not pull off everything with a signal-slot mechanism.
It's an application that should run on RaspberryPi. Usually (in C compilers) I did it likeQt is an asynchronous C++ framework.
If you want to use Qt, you have to adapt your code to Qt.
Here are some good practices about Qt and threading https://www.kdab.com/the-eight-rules-of-multithreaded-qt/The under layered hardware is not that important. Embedded devices like RPi or power full PC , this only change the performances but not the programming style.
Signals/slots are there to made the classes as modular as possible.
With this mechanism, you can dynamically "route" information between all modules.
It also helps for multithreading, all required data copy are done when signal is "emitted".With
QObject
parent/child mechanism, you can simplify memory management, because on parent destruction, all children will be automatically destroyed.Often, there is no need to create an extra thread, because of asynchronous nature of Qt. Just ensure main thread is not locked by a forever loop so the event queue can run and process the signals/slots.
-
@KroMignon
Thank you. One more question - how do I run periodical tasks? Not tied to any event, they should run constantly. -
@jenya7 said in How to share one signal for two slots.:
how do I run periodical tasks? Not tied to any event, they should run constantly.
With
QTimer
==> https://doc.qt.io/qt-5/qtimer.html -
@KroMignon said in How to share one signal for two slots.:
@jenya7 said in How to share one signal for two slots.:
how do I run periodical tasks? Not tied to any event, they should run constantly.
With
QTimer
==> https://doc.qt.io/qt-5/qtimer.htmlBut where? Where do I create a scheduler? In main.cpp?
int main(int argc, char *argv[]) { QApplication a(argc, argv); MSGPARSER m_msgparser; MainWindow w; w.show(); QObject::connect(&w.wifi_udp, &MyUDP::GetUdpMessage, &m_msgparser, &MSGPARSER::GetUdpMessage); return a.exec(); }
-
@jenya7 said in How to share one signal for two slots.:
But where? Where do I create a scheduler? In main.cpp?
Where ever you want:
- in main
int main(int argc, char *argv[]) { QApplication a(argc, argv); QTimer tmr; QObject::connect(&tmr, &QTimer::timeout, []{) { // do your stuff here }); tmr.start(1000); // every second for example ... return a.exec(); }
- in a class
auto tmr = new QTimer(this);// use a parent to ensure timer is deleted when instance is destroyed QObject::connect(&tmr, &QTimer::timeout, this, &ThisClass:timerSlot); tmr->start(1000);
There are many options!
-
Thank you.