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. connect doesn't call a slot but invokes lambda

connect doesn't call a slot but invokes lambda

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 849 Views
  • 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.
  • SeeLookS Offline
    SeeLookS Offline
    SeeLook
    wrote on last edited by SeeLook
    #1

    Hi,

    Could someone enlighten me what I'm doing wrong that basic connect syntax:

    connect(myObj, &MyObjClass::signal1, this, &ThisClass::signal1Slot);
    

    doesn't work.

    (No matter ThisClass::signal1Slot() method is ordinary method or marked as slots)
    but

    connect(myObj, &MyObjClass::signal1, [=]{ signal1Slot(); });
    

    is invoked as intended

    but

    connect(myObj, &MyObjClass::signal1, this, [=]{ signal1Slot(); });
    

    doesn't.

    Also 'old syntax' with SIGNAL and SLOT keywords doesn't work.

    One thing could be a key here: signal1 is called from another thread (and not QThread subclass) than the receiver thread.

    JKSHJ 1 Reply Last reply
    0
    • mranger90M Offline
      mranger90M Offline
      mranger90
      wrote on last edited by
      #2

      You'll have to provide a little more information. What do you mean by "doesn't work". It doesn't compile ? Does it compile but the slot is not being called ?
      Are there any debug messages being printed about the connect call ?
      Can you show the signatures of the signal and the slot ?

      1 Reply Last reply
      0
      • SeeLookS Offline
        SeeLookS Offline
        SeeLook
        wrote on last edited by
        #3

        To clarify more:
        Signatures are as I wrote:

        connect(myObj, &MyObjClass::signal1, this, &ThisClass::signal1Slot); // doesn't work
        connect(myObj, &MyObjClass::signal1, [=]{ signal1Slot(); }); // only one that works
        connect(myObj, &MyObjClass::signal1, this, [=]{ signal1Slot(); }); // doesn't
        
        

        All options above compiles fine, any message about that connections either during compilation or runtime. Any troublesome message at all :-)

        With old syntax also there is no any runtime warning and of course in this case `slots' keyword is used above slot method declaration.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Maybe a silly question but, how do you know it's not called ?
          Can you show the complete code that triggers this ?
          What version of Qt are you using ?
          On what platform ?
          What compiler ?

          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
          0
          • SeeLookS Offline
            SeeLookS Offline
            SeeLook
            wrote on last edited by SeeLook
            #5

            I'm on Arch Linux with Qt 5.13. My app is compilled with GCC 9.1.
            And simply I set debug message inside the slot method.

            This "other thread" signal is emitted from audio callback function. (RtAudio actually) and by debug message before emit signal1(); I can detect it is indeed invoked.

            In general all works as intended, but just for my knowledge I'm wondering why only that particular way of connection is respected.

            Shall I write even more?

            1 Reply Last reply
            0
            • SeeLookS SeeLook

              Hi,

              Could someone enlighten me what I'm doing wrong that basic connect syntax:

              connect(myObj, &MyObjClass::signal1, this, &ThisClass::signal1Slot);
              

              doesn't work.

              (No matter ThisClass::signal1Slot() method is ordinary method or marked as slots)
              but

              connect(myObj, &MyObjClass::signal1, [=]{ signal1Slot(); });
              

              is invoked as intended

              but

              connect(myObj, &MyObjClass::signal1, this, [=]{ signal1Slot(); });
              

              doesn't.

              Also 'old syntax' with SIGNAL and SLOT keywords doesn't work.

              One thing could be a key here: signal1 is called from another thread (and not QThread subclass) than the receiver thread.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @seelook said in connect doesn't call a slot but invokes lambda:

              connect(myObj, &MyObjClass::signal1, [=]{ signal1Slot(); });
              

              is invoked as intended

              This connection runs the lambda in myObj's thread.

              All your other connections try to run the lambda/slot in this's thread.

              One thing could be a key here: signal1 is called from another thread (and not QThread subclass) than the receiver thread.

              Does this live in a thread that has a running, non-blocked event loop?

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              6
              • SeeLookS Offline
                SeeLookS Offline
                SeeLook
                wrote on last edited by SeeLook
                #7

                I forgot to explain one thing, but I forgot it for myself as well:
                Year ago I moved this to its own thread (audio data preparing heavy load), but I just forgot. Sorry.
                But this explains everything.

                connect(myObj, &MyObjClass::signal1, [=]{ signal1Slot(); });
                

                This connection runs the lambda in myObj's thread.

                This is so obvious that I even didn't think about it. It expalins how it works.
                @JKSH Thank You a lot!
                But this is first conclusion.
                Another one is:

                               main thread (event loop)
                                        ^        \
                                       /          \
                                      /            \         
                         thread_A(sender)      thread_B(receiver)
                

                Signal sent by sender in thread A has to pass trough main thread to be delivered to receiver in thread B, in other words somewhere in main thread has to be receiver of thread A signal which will invoke slot in thread B (or will forward signal to it). Makes sense.
                So I have to manage that.

                JKSHJ 1 Reply Last reply
                0
                • SeeLookS SeeLook

                  I forgot to explain one thing, but I forgot it for myself as well:
                  Year ago I moved this to its own thread (audio data preparing heavy load), but I just forgot. Sorry.
                  But this explains everything.

                  connect(myObj, &MyObjClass::signal1, [=]{ signal1Slot(); });
                  

                  This connection runs the lambda in myObj's thread.

                  This is so obvious that I even didn't think about it. It expalins how it works.
                  @JKSH Thank You a lot!
                  But this is first conclusion.
                  Another one is:

                                 main thread (event loop)
                                          ^        \
                                         /          \
                                        /            \         
                           thread_A(sender)      thread_B(receiver)
                  

                  Signal sent by sender in thread A has to pass trough main thread to be delivered to receiver in thread B, in other words somewhere in main thread has to be receiver of thread A signal which will invoke slot in thread B (or will forward signal to it). Makes sense.
                  So I have to manage that.

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @seelook said in connect doesn't call a slot but invokes lambda:

                                 main thread (event loop)
                                          ^        \
                                         /          \
                                        /            \         
                           thread_A(sender)      thread_B(receiver)
                  

                  Signal sent by sender in thread A has to pass trough main thread to be delivered to receiver in thread B, in other words somewhere in main thread has to be receiver of thread A signal which will invoke slot in thread B (or will forward signal to it).

                  In your diagram, Thread B must still have a running event loop. If it doesn't have an event loop, then Thread B can't receive any signals and can't have any slot invoked.

                  A signal can go straight from Thread A to Thread B without going through the main thread. Thread B just needs a running event loop.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  2

                  • Login

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