Nominate our 2022 Qt Champions!

Signal/slots in different threads and const reference?

  • Hello all,

    Is it okay to have a slot that will be called from a different thread and that takes as its first argument a const QByteArray&?
    For instance, let say I have a Receiver class (inheriting from QObject):

    class Receiver : public QObject
        Receiver(QObject* parent = nullptr);
    public slots:
        void dataReceived(const QByteArray& data);
        QThread m_thread;

    and the constructor is defined as:

    Receiver::Receiver(QObject* parent) : QObject(parent)

    When the onDataReceived slot will be called from an object living in another thread than the m_thread above, is it guaranteed that the QBytearray will live long enough?

    Thank you!

  • Moderators

    @mistralegna Qt::QueuedConnections do implicit copying, so even when its marked const ref, the byte array will be copied and therefore live long enough

Log in to reply