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 I use MyClass& in Qt signal and slots?

can I use MyClass& in Qt signal and slots?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 629 Views 2 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.
  • MozzieM Offline
    MozzieM Offline
    Mozzie
    wrote on last edited by
    #1

    I am curious, if I emit a signal with parameter MyClass& which is a stack variable from one thread and I receive this signal from another thread and use Qt::QueuedConnection, is there any problem? would it crash by memory access? if not , how did this work?

    thank you very much!

    JKSHJ 1 Reply Last reply
    0
    • MozzieM Mozzie

      I am curious, if I emit a signal with parameter MyClass& which is a stack variable from one thread and I receive this signal from another thread and use Qt::QueuedConnection, is there any problem? would it crash by memory access? if not , how did this work?

      thank you very much!

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Mozzie said in can I use MyClass& in Qt signal and slots?:

      I am curious, if I emit a signal with parameter MyClass& which is a stack variable from one thread and I receive this signal from another thread and use Qt::QueuedConnection, is there any problem? would it crash by memory access? if not , how did this work?

      If you emit const MyClass&, then Qt will copy the variable so that it can be safely used by the other thread. (You need to register your class first, using qRegisterMetaType(): https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType )

      Don't use a non-const reference between threads.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      MozzieM 1 Reply Last reply
      2
      • JKSHJ JKSH

        @Mozzie said in can I use MyClass& in Qt signal and slots?:

        I am curious, if I emit a signal with parameter MyClass& which is a stack variable from one thread and I receive this signal from another thread and use Qt::QueuedConnection, is there any problem? would it crash by memory access? if not , how did this work?

        If you emit const MyClass&, then Qt will copy the variable so that it can be safely used by the other thread. (You need to register your class first, using qRegisterMetaType(): https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType )

        Don't use a non-const reference between threads.

        MozzieM Offline
        MozzieM Offline
        Mozzie
        wrote on last edited by
        #3

        @JKSH said in can I use MyClass& in Qt signal and slots?:

        @Mozzie said in can I use MyClass& in Qt signal and slots?:

        I am curious, if I emit a signal with parameter MyClass& which is a stack variable from one thread and I receive this signal from another thread and use Qt::QueuedConnection, is there any problem? would it crash by memory access? if not , how did this work?

        If you emit const MyClass&, then Qt will copy the variable so that it can be safely used by the other thread. (You need to register your class first, using qRegisterMetaType(): https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType )

        Don't use a non-const reference between threads.

        thank you. So, if I use const MyClass& as paremeter, I can use it between threads?

        JKSHJ 1 Reply Last reply
        0
        • MozzieM Mozzie

          @JKSH said in can I use MyClass& in Qt signal and slots?:

          @Mozzie said in can I use MyClass& in Qt signal and slots?:

          I am curious, if I emit a signal with parameter MyClass& which is a stack variable from one thread and I receive this signal from another thread and use Qt::QueuedConnection, is there any problem? would it crash by memory access? if not , how did this work?

          If you emit const MyClass&, then Qt will copy the variable so that it can be safely used by the other thread. (You need to register your class first, using qRegisterMetaType(): https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType )

          Don't use a non-const reference between threads.

          thank you. So, if I use const MyClass& as paremeter, I can use it between threads?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @Mozzie said in can I use MyClass& in Qt signal and slots?:

          So, if I use const MyClass& as paremeter, I can use it between threads?

          Yes (assuming that your class is allowed to be copied between threads).

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          JonBJ 1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            [Side discussion forked to https://forum.qt.io/topic/125858/passing-qobjects-across-threads-via-signals-and-slots ]

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            2

            • Login

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