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. Signal/slots in different threads and const reference?
Forum Updated to NodeBB v4.3 + New Features

Signal/slots in different threads and const reference?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 384 Views 1 Watching
  • 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.
  • mistralegnaM Offline
    mistralegnaM Offline
    mistralegna
    wrote on last edited by mistralegna
    #1

    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!

    J.HilkJ 1 Reply Last reply
    0
    • mistralegnaM mistralegna

      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!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      5

      • Login

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