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. [SOLVED] Connecting signal and slot between parent and "grandchild"

[SOLVED] Connecting signal and slot between parent and "grandchild"

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 15.7k 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.
  • R Offline
    R Offline
    rizoritis
    wrote on last edited by
    #1

    Hello,

    I have an application with a main widget (parentWidget). Within my main widget I create a new widget (we can call it childWidget). Within childWidget I create a new widget (call it grandchildWidget). I want to connect a signal from my grandchild to my parent widget. I tried using:

    @connect(this, SIGNAL(updateGeneralStatusSignal()), this->parent()->parent(), SLOT(updateGeneralStatusSlot()));@

    however, this did not work. I have been able to use this->parent() between the parent and child widget, but it wont allow me to do it between the parent and grandchild.

    Any thoughts?

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You're doing it the wrong way.

      You should emit, if necessary chain the emit in the child widget, and connect the childWidget signal in parentWidget.

      i.e.

      @
      parentWidget.cpp:
      connect(childWidget, SIGNAL(updateGeneralStatusSignal()), SLOT(updateGeneralStatusSlot()));

      childWidget.h:

      class ChildWidget : public QObject
      {
      Q_OBJECT

      public:

      signals:
      void updateGeneralStatusSignal();
      }

      childWidget.cpp:
      connect(grandChild, SIGNAL(updateGeneralStatusSignal()), SIGNAL(updateGeneralStatusSignal()));
      @

      Your grandChild widget should not care about connecting other widgets to him. Otherwise you'll break orthogonality.

      It's the object who knows where the signal should go the should the connections.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        Hi,

        IMO, the reason is that, this->parent()->parent() isn't the widget as you expected. You can output the result of

        @
        this->parent()->parent()->metaObject()->className()
        @


        BTW, usually it's not good to use this style for widgets in a real application, as it's hard to maintain. You can pass the pointer of the expected widget to your current widget.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rizoritis
          wrote on last edited by
          #4

          [quote author="SGaist" date="1373559965"]Hi,

          You're doing it the wrong way.

          You should emit, if necessary chain the emit in the child widget, and connect the childWidget signal in parentWidget.

          i.e.

          @
          parentWidget.cpp:
          connect(childWidget, SIGNAL(updateGeneralStatusSignal()), SLOT(updateGeneralStatusSlot()));

          childWidget.h:

          class ChildWidget : public QObject
          {
          Q_OBJECT

          public:

          signals:
          void updateGeneralStatusSignal();
          }

          childWidget.cpp:
          connect(grandChild, SIGNAL(updateGeneralStatusSignal()), SIGNAL(updateGeneralStatusSignal()));
          @

          Your grandChild widget should not care about connecting other widgets to him. Otherwise you'll break orthogonality.

          It's the object who knows where the signal should go the should the connections.[/quote]

          Perfect! This worked. Thanks!

          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