Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. emit from another thread/function?
Qt 6.11 is out! See what's new in the release blog

emit from another thread/function?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zwokaros
    wrote on last edited by
    #1

    Hello

    I'm using threads and I can finally have a smooth GUI

    Anyway at some part I can't emit

    emit packetEvent(QString("hello"));
        pcap_loop(fp, 0, packet_handler, NULL);
    

    (it's a sniffer)

    So I can emit before calling the pcap_loop function, but how do I emit from the packet_loop function?

    Thanks

    aha_1980A 1 Reply Last reply
    0
    • Z zwokaros

      Hello

      I'm using threads and I can finally have a smooth GUI

      Anyway at some part I can't emit

      emit packetEvent(QString("hello"));
          pcap_loop(fp, 0, packet_handler, NULL);
      

      (it's a sniffer)

      So I can emit before calling the pcap_loop function, but how do I emit from the packet_loop function?

      Thanks

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi @zwokaros

      Anyway at some part I can't emit

      emit packetEvent(QString("hello"));
          pcap_loop(fp, 0, packet_handler, NULL);
      

      (it's a sniffer)

      So I can emit before calling the pcap_loop function, but how do I emit from the packet_loop function?

      how about doing it from the packet_handler callback?

      Qt has to stay free or it will die.

      Z 1 Reply Last reply
      0
      • aha_1980A aha_1980

        hi @zwokaros

        Anyway at some part I can't emit

        emit packetEvent(QString("hello"));
            pcap_loop(fp, 0, packet_handler, NULL);
        

        (it's a sniffer)

        So I can emit before calling the pcap_loop function, but how do I emit from the packet_loop function?

        how about doing it from the packet_handler callback?

        Z Offline
        Z Offline
        zwokaros
        wrote on last edited by
        #3

        @aha_1980

        I'm not sure I can do that

        packet_handler will be called everytime I recieve a packet

        void    packet_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
        

        So I'm not sure how to do it

        aha_1980A 1 Reply Last reply
        0
        • Z zwokaros

          @aha_1980

          I'm not sure I can do that

          packet_handler will be called everytime I recieve a packet

          void    packet_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
          

          So I'm not sure how to do it

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @zwokaros said in emit from another thread/function?:

          @aha_1980

          I'm not sure I can do that

          packet_handler will be called everytime I recieve a packet

          ... and isn't that what you want to do, emit when a packet arrived?

          Qt has to stay free or it will die.

          Z 1 Reply Last reply
          0
          • aha_1980A aha_1980

            @zwokaros said in emit from another thread/function?:

            @aha_1980

            I'm not sure I can do that

            packet_handler will be called everytime I recieve a packet

            ... and isn't that what you want to do, emit when a packet arrived?

            Z Offline
            Z Offline
            zwokaros
            wrote on last edited by
            #5

            @aha_1980
            It is but I don't know how to implement it

            kshegunovK 1 Reply Last reply
            0
            • Z zwokaros

              @aha_1980
              It is but I don't know how to implement it

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6
              pcap_loop(fp, 0, packet_handler, this); //< the current object goes as additional data.
              
              void packet_handler(u_char * object, const struct pcap_pkthdr *header, const u_char *pkt_data)
              {
                 WhateverTheClassWas * emitter = reinterpret_cast<WhateverTheClassWas *>(object);
                 emit emitter->packetEvent(QString("Whatever"));
                 // ...
              }
              

              Read the documentation carefully, it says you can pass user data to the callback. User data means a pointer to some unspecified datastructure (an opaque pointer). It's your responsibility to manage the lifetime and to do the conversions to/from the correct type, however you can pass practically any data set to the callback.

              Read and abide by the Qt Code of Conduct

              Z 1 Reply Last reply
              3
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Following @kshegunov's suggestion, I'd rather go with QMetaObject::invokeMethod. It will be clearer and cleaner. Emitting signal from another class is not something you should do. IIRC, they were even declared as protected in the past.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3
                • kshegunovK kshegunov
                  pcap_loop(fp, 0, packet_handler, this); //< the current object goes as additional data.
                  
                  void packet_handler(u_char * object, const struct pcap_pkthdr *header, const u_char *pkt_data)
                  {
                     WhateverTheClassWas * emitter = reinterpret_cast<WhateverTheClassWas *>(object);
                     emit emitter->packetEvent(QString("Whatever"));
                     // ...
                  }
                  

                  Read the documentation carefully, it says you can pass user data to the callback. User data means a pointer to some unspecified datastructure (an opaque pointer). It's your responsibility to manage the lifetime and to do the conversions to/from the correct type, however you can pass practically any data set to the callback.

                  Z Offline
                  Z Offline
                  zwokaros
                  wrote on last edited by
                  #8

                  @kshegunov

                  Thanks a lot for your answer!

                  Allthough I couldn't pass this as an argument because of the pcap library, I made a global variable from which I emit in the packet_handler function!

                  aha_1980A kshegunovK 2 Replies Last reply
                  0
                  • Z zwokaros

                    @kshegunov

                    Thanks a lot for your answer!

                    Allthough I couldn't pass this as an argument because of the pcap library, I made a global variable from which I emit in the packet_handler function!

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @zwokaros said in emit from another thread/function?:

                    @kshegunov

                    Thanks a lot for your answer!

                    Allthough I couldn't pass this as an argument because of the pcap library,

                    why not? sure you can!

                    I made a global variable from which I emit in the packet_handler function!

                    that works in your case, but breaks when you have multiple callbacks.

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    1
                    • Z zwokaros

                      @kshegunov

                      Thanks a lot for your answer!

                      Allthough I couldn't pass this as an argument because of the pcap library, I made a global variable from which I emit in the packet_handler function!

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @SGaist is correct as usual, QMetaObject::invokeMethod would indeed be preferred to emitting directly.

                      @zwokaros said in emit from another thread/function?:

                      I made a global variable from which I emit in the packet_handler function!

                      To add to @aha_1980, this also breaks reentrancy, so if you're working with multiple threads it'd hurt - you'd need to manually synchronize access. Just pass the object as additional data following the docs is my advice.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      3

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved