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. Connect to signal emitted from reference.

Connect to signal emitted from reference.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 647 Views
  • 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.
  • H Offline
    H Offline
    hsolter
    wrote on last edited by
    #1

    Is there a reason for not providing a connect method overload to connect a signal from reference?
    For example;

    MyClass::MyClass(OtherClass& ref): m_refmember(ref)
    {
        connect (m_refmember, &OtherClass::someSignal,
                 this, &MyClass::someSlot);
    }
    
    

    Assume that I don't know the internals of the connect mechanism.

    JKSHJ raven-worxR 2 Replies Last reply
    0
    • H hsolter

      Is there a reason for not providing a connect method overload to connect a signal from reference?
      For example;

      MyClass::MyClass(OtherClass& ref): m_refmember(ref)
      {
          connect (m_refmember, &OtherClass::someSignal,
                   this, &MyClass::someSlot);
      }
      
      

      Assume that I don't know the internals of the connect mechanism.

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

      @hsolter said in Connect to signal emitted from reference.:

      Is there a reason for not providing a connect method overload to connect a signal from reference?

      Mainly because Qt encourages the convention of allocating QObjects using new (except for top-level, parentless objects). You can allocate all your QObjects on the stack if you want, but be careful of construction/destruction order to avoid crashes: http://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

      Also, adding reference-based overloads would almost quadruple (4x) the total number of overloads:

      • Pointer sender, pointer receiver
      • Pointer sender, reference receiver
      • Reference sender, pointer receiver
      • Reference sender, reference receiver

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

      kshegunovK 1 Reply Last reply
      3
      • JKSHJ JKSH

        @hsolter said in Connect to signal emitted from reference.:

        Is there a reason for not providing a connect method overload to connect a signal from reference?

        Mainly because Qt encourages the convention of allocating QObjects using new (except for top-level, parentless objects). You can allocate all your QObjects on the stack if you want, but be careful of construction/destruction order to avoid crashes: http://doc.qt.io/qt-5/objecttrees.html#construction-destruction-order-of-qobjects

        Also, adding reference-based overloads would almost quadruple (4x) the total number of overloads:

        • Pointer sender, pointer receiver
        • Pointer sender, reference receiver
        • Reference sender, pointer receiver
        • Reference sender, reference receiver
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @JKSH said in Connect to signal emitted from reference.:

        Mainly because Qt encourages the convention of allocating QObjects using new (except for top-level, parentless objects).

        I don't believe it "encourages" this, only that it's the conventional way to do it since QObject can't be copied. In fact allocating in the stack will always outperform the additional call to the heap manager and makes memory leaks absolutely impossible. You do need, however, as you noted, to ensure the objects are in a stack - i.e. first-in-last-out.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • H hsolter

          Is there a reason for not providing a connect method overload to connect a signal from reference?
          For example;

          MyClass::MyClass(OtherClass& ref): m_refmember(ref)
          {
              connect (m_refmember, &OtherClass::someSignal,
                       this, &MyClass::someSlot);
          }
          
          

          Assume that I don't know the internals of the connect mechanism.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @hsolter said in Connect to signal emitted from reference.:

          Is there a reason for not providing a connect method overload to connect a signal from reference?

          is there any reason to do so?
          I mean you just need to add a single character to your code:

          connect (&m_refmember, &OtherClass::someSignal, this, &MyClass::someSlot);
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          4

          • Login

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