Using QThread , Signal() , Slot() So GUI won't freeze
-
I'm wondering if there is a better way to accomplish this task maybe using Tins .. but that's and issue by itself
@__d4ve__ said in Using QThread , Signal() , Slot() So GUI won't freeze:
I'm wondering if there is a better way to accomplish this task maybe using Tins
How should this change something? It doesn't matter what lib you use, whether it's pcap, tins or packet sniffer 9000. If you want to keep your GUI responsive while using a synchronous library or function, you have to move it to a different thread.
Threading in general is a "not-so-easy" task, esp. with limited programming knowledge, but IMO a Worker-Thread is one of the easier approaches and it matches your requirements as well.
Read about how it works (in this topic are already plently of links) and be aware of what you really want to do (what function you want to run? what data should be send from where to where?... etc.)
-
@__d4ve__ said in Using QThread , Signal() , Slot() So GUI won't freeze:
I'm wondering if there is a better way to accomplish this task maybe using Tins
How should this change something? It doesn't matter what lib you use, whether it's pcap, tins or packet sniffer 9000. If you want to keep your GUI responsive while using a synchronous library or function, you have to move it to a different thread.
Threading in general is a "not-so-easy" task, esp. with limited programming knowledge, but IMO a Worker-Thread is one of the easier approaches and it matches your requirements as well.
Read about how it works (in this topic are already plently of links) and be aware of what you really want to do (what function you want to run? what data should be send from where to where?... etc.)
-
The task is keep GUI responsive , and yes you are right we achieved at this point the threading stuff the issue now have changed into running pcap_loop() which isn't working because it requires static function and I use non-static function
@__d4ve__ said in Using QThread , Signal() , Slot() So GUI won't freeze:
which isn't working because it requires static function and I use non-static function
This is probably not the reason behind this issue...
I can only repeat what @JonB and others have said...
Look at the lines 55 and 88 and check what's written there or post it here. Because it's a pain to check your linked archieve when you have the code right in front of you. So probably nobody will dig through your whole project to find these two or more lines which will produce your current error. -
@__d4ve__ said in Using QThread , Signal() , Slot() So GUI won't freeze:
which isn't working because it requires static function and I use non-static function
This is probably not the reason behind this issue...
I can only repeat what @JonB and others have said...
Look at the lines 55 and 88 and check what's written there or post it here. Because it's a pain to check your linked archieve when you have the code right in front of you. So probably nobody will dig through your whole project to find these two or more lines which will produce your current error. -
line 55 :
emit update_gui(packetInfoQString);line 88 :
pcap_loop(handle ,NUM_PACKETS_TO_CAPTURE ,packet_handler , nullptr);@__d4ve__ said in Using QThread , Signal() , Slot() So GUI won't freeze:
emit update_gui(packetInfoQString);
Now, look at the first error message:
"\Project 1 - Network Packet GUI\Packet_Capture_GUI\workerobject.cpp:55: error: C3861: 'update_gui': identifier not found"It tells you that update_gui is not known. Did you declare update_gui as signal in that class?
-
@__d4ve__ said in Using QThread , Signal() , Slot() So GUI won't freeze:
emit update_gui(packetInfoQString);
Now, look at the first error message:
"\Project 1 - Network Packet GUI\Packet_Capture_GUI\workerobject.cpp:55: error: C3861: 'update_gui': identifier not found"It tells you that update_gui is not known. Did you declare update_gui as signal in that class?
Yes,
signals: void update_gui(QString packetInfoQString); };The issue is here : pcap_loop got call back function named packet_handle - this function emit update_gui
but the problem here that pcap_loop requires static function , while packet_handle can't be static function
try to run the project that's very short code
-
Yes,
signals: void update_gui(QString packetInfoQString); };The issue is here : pcap_loop got call back function named packet_handle - this function emit update_gui
but the problem here that pcap_loop requires static function , while packet_handle can't be static function
try to run the project that's very short code
@__d4ve__
If you mean that signalupdate_gui()is a member ofMainWindowthen you won't be able to call it viaWorkerObject* wo=qobject_cast<WorkerObject*>(user_data); assert(wo); wo->update_gui(...);You presumably need the signal defined somewhere accessible from
WorkerObjectand then a slot inMainWindowto receive the signal from the worker object.Get the principle suggested by @mpergand working.
-
Yes,
signals: void update_gui(QString packetInfoQString); };The issue is here : pcap_loop got call back function named packet_handle - this function emit update_gui
but the problem here that pcap_loop requires static function , while packet_handle can't be static function
try to run the project that's very short code
@__d4ve__ said in Using QThread , Signal() , Slot() So GUI won't freeze:
signals:
void update_gui(QString packetInfoQString);
};Where is this signal defined?
In the code snippet you've posted here, you have a function called
(I dont know if this is still your current code...)void workerobject::packet_looper() { .... }so one can assume it's a part of your
workerobject.cppfile.
Above that code there isvoid packet_handler(u_char* user_data, const struct pcap_pkthdr* packet_header, const u_char* packet_data) { // ..... emit update_gui(packetInfoQString); }Are you aware that
packet_handlerwhen written like that, is not a member of any class? It's defined as global function and there it probably wont recognize whatupdate_guiis.
The same applies, when trying to emit that signal from somewhere else other than where it was defined. -
@__d4ve__ said in Using QThread , Signal() , Slot() So GUI won't freeze:
signals:
void update_gui(QString packetInfoQString);
};Where is this signal defined?
In the code snippet you've posted here, you have a function called
(I dont know if this is still your current code...)void workerobject::packet_looper() { .... }so one can assume it's a part of your
workerobject.cppfile.
Above that code there isvoid packet_handler(u_char* user_data, const struct pcap_pkthdr* packet_header, const u_char* packet_data) { // ..... emit update_gui(packetInfoQString); }Are you aware that
packet_handlerwhen written like that, is not a member of any class? It's defined as global function and there it probably wont recognize whatupdate_guiis.
The same applies, when trying to emit that signal from somewhere else other than where it was defined.To add to my fellows, it's typically a good case for the use of invokeMethod.