Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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
    {
        Q_OBJECT:
    
    public:
        Receiver(QObject* parent = nullptr);
    
    public slots:
        void dataReceived(const QByteArray& data);
    
    private:
        QThread m_thread;
    }
    

    and the constructor is defined as:

    Receiver::Receiver(QObject* parent) : QObject(parent)
    {
        moveToThread(&m_thread);
    }
    

    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