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. C++ object composition and signals and slots
Forum Updated to NodeBB v4.3 + New Features

C++ object composition and signals and slots

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.8k 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.
  • D Offline
    D Offline
    ddelta
    wrote on last edited by
    #1

    I am having some issues using signals and slots with object composition

    I have two objects ObjectA and ObjectB
    ObjectA has a member of type ObjectB. Both objects inherit from QObject.
    ObjectA and ObjectB has both signals and slots. When I attempt to connect the signals or slots in ObjectB using
    @
    QObject::connect(this,SIGNAL(functionSignal(int,int)), ObjectA.ObjectB, SLOT(ObSlot(int,int)) ) );
    QObject::connect(ObjectA.ObjectB,SIGNAL(ObSignal(int,int)),),this, SLOT(functionSlot(int,int)) ) );
    @

    Then QT doesn’t seem to recognizing any of the signals or slots contained in ObjectB…..

    Is there something I am missing here?

    How does QT handle connecting signals and slots when using object composition? Is there something special I need to do to get this going?

    does the MOC compiler know about signals and slot from objects that are composed members of other objects?

    btw,

    when I preview this post in the preview windows it seems to be removing the argument inside the SIGNAL or SLOT macros... not sure why this is happening..

    [andreyc EDIT]: Added @ around the code.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      Is ObjectB a pointer?
      Are there any warnings or errors during compilation or execution of your app?

      Could you post how do you declare and instantiate ObjectB.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on last edited by
        #3

        [quote]when I preview this post in the preview windows it seems to be removing the argument inside the SIGNAL or SLOT macros… not sure why this is happening.. [/quote]

        You need to put @ characters around your code to make it looks nice.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          ddelta
          wrote on last edited by
          #4

          Ok so i believe I answered my own question so I thought i should post here to explain.

          What I noticed is that it doesn't seem like I can connect signals and slots when the object that contains the signals or slots are instaiated on the stack. Meaning that if they are not allocated as pointer to objects created with the "new" operator on the heap. For example

          This will work

          ClassA *classA_inst = new ClassA();
          ClassC *classC_inst = new ClassC();

          QObject::connect(classA_inst,SIGNAL(classASignal1()),classC_inst,SLOT(classCSlot1()) );

          But if I do this:

          ClassA classA_inst;
          ClassC classC_inst;

          QObject::connect(classA_inst,SIGNAL(classASignal1()),classC_inst,SLOT(classCSlot1()) );

          This wont works.

          I am sure this was obvious to most here, but i was not aware that you cannot using signals and slots when the object was created on the stack....

          Is this correct? I am I sane?

          If this is correct why is this? why do we have to using the heap to create the objects for using signals and slots?

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Please use code tags for code.

            Of course you can use stack based variables. Look at the connect() signature. It takes a pointer so in case of a stack based variable you need an address of it:
            @
            X* ptr = ...
            X nonptr = ...

            connect(ptr, ...
            connect(&nonptr, ...

            connect(ptr->somePtrMember, ...
            connect(&ptr->someNonPtrMember, ...

            connect(nonptr.somePtrMember, ...
            connect(&nonptr.someNonPtrMember, ...
            @
            That's basic c++ ;)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

              And another "basic C++" to remember that as soon as control flow leaves a function all stack based variables are destroyed.
              So a signal will never be fired or a slot will never be called or an app will get an exception.

              1 Reply Last reply
              0

              • Login

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