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

  • 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);
    void PBXReader::gotParsingTextMessageFromSMSHandler(QSharedPointer<Message> msg)
        emit parsingTextMessageFinShared(msg);
        emit parsingTextMessageFin();

    and in the main thread:

    public slots:
        void gotParsingTextMessage(QSharedPointer<Message>);
        void gotParsingTextMesg();

    Does anyone got any idea why?

  • Qt Champions 2016


    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 ?

  • @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);

  • Qt Champions 2016

    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)

  • @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());


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

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

  • Qt Champions 2016

    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

  • 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 :)

  • Qt Champions 2016

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

  • 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.

  • Qt Champions 2016


    Well, seems its all explained here
    I guess that Auto Connection does guess. :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.