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. Can't pass a signal with QSharedPointer (or std::shared_ptr<>) between threads
Forum Update on Monday, May 27th 2025

Can't pass a signal with QSharedPointer (or std::shared_ptr<>) between threads

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 8.5k 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.
  • M Offline
    M Offline
    michalos
    wrote on 27 Feb 2017, 14:05 last edited by michalos
    #1

    Hi
    I'm trying to catch a signal emitted with a shared pointer, but the signal is not being caught in a class that is in a different thread.
    When I try to catch an empty signal, everything works fine.

    My code :

    //creating the QShared ptr
    //   std::shared_ptr<Message> msgShared (new Message());
    
        QSharedPointer<Message> message(new Message());
    
    // emiting : (works)
                emit parsingTextMessageFinished(message);
    
    //different class:
    
     void PBXReader::initHandlers(){
     smsHandler = new SMSTagHandler(req_ans_mangr, this);
    
      connect(smsHandler,&SMSTagHandler::parsingTextMessageFinished,this,&PBXReader::gotParsingTextMessageFromSMSHandler);
    
    }
    
    void PBXReader::gotParsingTextMessageFromSMSHandler(QSharedPointer<Message> msg)
    {
        emit parsingTextMessageFinShared(msg);
        emit parsingTextMessageFin();
    
    }
    

    and in the main thread:

    public slots:
        void gotParsingTextMessage(QSharedPointer<Message>);
        void gotParsingTextMesg();
    
        connect(pbxReader,&PBXReader::parsingTextMessageFin,this,&MessengerCTI::gotParsingTextMesg);
        connect(pbxReader,&PBXReader::parsingTextMessageFinShared,this,&MessengerCTI::gotParsingTextMessage);
    

    Does anyone got any idea why?

    M 1 Reply Last reply 27 Feb 2017, 14:37
    0
    • M michalos
      27 Feb 2017, 14:05

      Hi
      I'm trying to catch a signal emitted with a shared pointer, but the signal is not being caught in a class that is in a different thread.
      When I try to catch an empty signal, everything works fine.

      My code :

      //creating the QShared ptr
      //   std::shared_ptr<Message> msgShared (new Message());
      
          QSharedPointer<Message> message(new Message());
      
      // emiting : (works)
                  emit parsingTextMessageFinished(message);
      
      //different class:
      
       void PBXReader::initHandlers(){
       smsHandler = new SMSTagHandler(req_ans_mangr, this);
      
        connect(smsHandler,&SMSTagHandler::parsingTextMessageFinished,this,&PBXReader::gotParsingTextMessageFromSMSHandler);
      
      }
      
      void PBXReader::gotParsingTextMessageFromSMSHandler(QSharedPointer<Message> msg)
      {
          emit parsingTextMessageFinShared(msg);
          emit parsingTextMessageFin();
      
      }
      

      and in the main thread:

      public slots:
          void gotParsingTextMessage(QSharedPointer<Message>);
          void gotParsingTextMesg();
      
          connect(pbxReader,&PBXReader::parsingTextMessageFin,this,&MessengerCTI::gotParsingTextMesg);
          connect(pbxReader,&PBXReader::parsingTextMessageFinShared,this,&MessengerCTI::gotParsingTextMessage);
      

      Does anyone got any idea why?

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Feb 2017, 14:37 last edited by
      #2

      @michalos

      Hi normally
      Signals between threads is Qt::QueuedConnection
      and you must use type that is known how to queue.

      I wonder if QSharedPointer<Message>
      is seens as new type.

      You dont get any warnings ?

      1 Reply Last reply
      3
      • M Offline
        M Offline
        michalos
        wrote on 27 Feb 2017, 14:56 last edited by
        #3

        @mrjj
        Thank You for Your answer.
        I was wondering the same thing.
        I don't get any info, that I should register a metatype (qRegisterMetaType).
        I do not get any warnings nor debug info.
        When I use a C style pointer (as below) everything works fine.

            Message *message = new Message(this);
        
        M 1 Reply Last reply 27 Feb 2017, 15:02
        0
        • M michalos
          27 Feb 2017, 14:56

          @mrjj
          Thank You for Your answer.
          I was wondering the same thing.
          I don't get any info, that I should register a metatype (qRegisterMetaType).
          I do not get any warnings nor debug info.
          When I use a C style pointer (as below) everything works fine.

              Message *message = new Message(this);
          
          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 27 Feb 2017, 15:02 last edited by
          #4

          @michalos
          Yes it was the qRegisterMetaType i was thinking of.
          But it seems you are fully aware of this and would see any warning so its not htat.

          Next question is.

          Does it go out of scope? ( for some reason)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            michalos
            wrote on 27 Feb 2017, 15:13 last edited by michalos
            #5

            @mrjj
            That is a good question.
            I do not know if it's out of scope.

            If I create it like that:

            QSharedPointer<Message> message(new Message());
            

            or

            std::shared_ptr<Message> msgShared (new Message());
            

            will it go out of scope?
            Isn't it created on the heap?

            M 1 Reply Last reply 28 Feb 2017, 07:08
            0
            • M michalos
              27 Feb 2017, 15:13

              @mrjj
              That is a good question.
              I do not know if it's out of scope.

              If I create it like that:

              QSharedPointer<Message> message(new Message());
              

              or

              std::shared_ptr<Message> msgShared (new Message());
              

              will it go out of scope?
              Isn't it created on the heap?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 28 Feb 2017, 07:08 last edited by
              #6

              I'm not sure. ( its with new so ..)

              Is Message your own class?

              I wonder if you can put aDebug() or breakpoint in the destructor and see if it dies when copied..

              Also make sure you have Qt::QueuedConnection on your connects

              1 Reply Last reply
              3
              • M Offline
                M Offline
                michalos
                wrote on 28 Feb 2017, 08:17 last edited by
                #7

                I've changed the connection to Qt::QueuedConnection and it worked :)

                I thought that this is done automatically when connecting between threads, but in this case it didn't.

                Thank You for Your help :)

                M 1 Reply Last reply 28 Feb 2017, 08:32
                0
                • M michalos
                  28 Feb 2017, 08:17

                  I've changed the connection to Qt::QueuedConnection and it worked :)

                  I thought that this is done automatically when connecting between threads, but in this case it didn't.

                  Thank You for Your help :)

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 28 Feb 2017, 08:32 last edited by
                  #8

                  @michalos
                  Oh. super.
                  I do wonder how it could work with no QueuedConnection and
                  no QSharedPointer but I guess its just random :)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    michalos
                    wrote on 28 Feb 2017, 14:18 last edited by
                    #9

                    I make most of my signal and slot connections without specifying that it's a QueuedConnection (actually all exept this one) and everything works fine.

                    M 1 Reply Last reply 28 Feb 2017, 14:28
                    0
                    • M michalos
                      28 Feb 2017, 14:18

                      I make most of my signal and slot connections without specifying that it's a QueuedConnection (actually all exept this one) and everything works fine.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 28 Feb 2017, 14:28 last edited by
                      #10

                      @michalos

                      Well, seems its all explained here
                      http://doc.qt.io/qt-5/threads-qobject.html
                      I guess that Auto Connection does guess. :)

                      1 Reply Last reply
                      0

                      3/10

                      27 Feb 2017, 14:56

                      topic:navigator.unread, 7
                      • Login

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