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. Child and parent widget communication.
Qt 6.11 is out! See what's new in the release blog

Child and parent widget communication.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 5.0k 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.
  • A Offline
    A Offline
    aswal
    wrote on last edited by
    #1

    Hi.

    I would like to know how a child widget can send some signal to its parent widget?

    Thanks.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      puterk
      wrote on last edited by
      #2

      Hello,

      You can make the child emit a signal, and connect it to the parent`s slot.

      For your signal:
      in your child widget`s .h file:
      @signals:
      void somesignal(/arguments if any/); @

      in your in your child widget`s cpp:
      @emit somesignal(/arguments if any/); @

      For your slot:
      in your parent widget`s .h file:
      @
      private slots:
      void someslot(/arguments if any/);@

      in your parent widget`s cpp:
      @void someslot(/arguments if any/)
      {
      //do something
      }@

      Then connect:
      @connect (child, SIGNAL(somesignal(/arguments if any/)),
      this, SLOT(someslot(/arguments if any/)));@

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

        Hi Puterk,
        Thanks for the reply. Yes, you are right, however we could achieve this signal and slot mechanism only when we know we have separate classes for both.

        1)- What to do if I don't know the parent widget at compile time?
        2)- Means how to get parent of a child widget at run time?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          puterk
          wrote on last edited by
          #4

          Hello,

          I'm not sure what you are trying to do but perhaps you could put the slots and connect the signal to all the possible parent widgets. This is what I'm doing with a dialog that would have a different parent window during runtime.

          Maybe you could provide more information and maybe some codes that you have tried so we could understand more.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            [quote author="aswal" date="1387354146"]

            What to do if I don't know the parent widget at compile time?

            Means how to get parent of a child widget at run time?

            [/quote]

            To get the parent widget use QWidget::parentWidget()
            You can test the parent widget's type either with qobject_cast or QObject::inherits()
            @
            QWidget* parentWidget = childWidget->parentWidget();
            if( parentWidget->inherits("MyClass") )
            {
            MyClass* myParent = static_cast<MyClass*>(parentWidget);
            }

            //or directly

            MyClass* myParent = qobject_cast<MyClass*>(parentWidget); //returns 0 if could not be cast
            if( myParent )
            {
            ...
            }
            @

            --- 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
            0
            • A Offline
              A Offline
              aswal
              wrote on last edited by
              #6

              puterk & raven-worx, thanks for the reply.

              Here is my question in detail:
              1- I have developed a QT tab plugin.
              class QDESIGNER_WIDGET_EXPORT QTab : public QTabWidget
              {}
              2- I can drag it in qt designer and add new tab in it.
              3- What I want that if I drag another instance of tab plugin and drop over the earlier instance created in step 2.
              4- So on drop event I want to get the tab widget created in step 2 and access its member function,

              Thanks.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                You can, but it's not very modular. You'll end up with tight connections between your components.

                The general answer here is that at least the child should know nothing of its parent, unless the two are intimately bound. I get the feeling that that is not the case here. So, the child widget should just expose signals (and slots, if needed), and not care who its parent is.

                Normally, neither of the parties of a signal-slot connection should care about who it connects to. That is the responsibility of whoever knows about both of these objects at the same time. Often, the two parties in a signal-slot connection will not have a parent-child relationship at all, but will be in different parts of the same form for instance. In such cases, usually the parent form is responsible for creating the connection, as it has access to both widgets.

                Often, a parent has knowledge of who its children will be, but obviously not always. However, there must be a place where you do have knowledge of both objects. That is the place where you make the connection.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  aswal
                  wrote on last edited by
                  #8

                  thanks, however it doesn't solve the issue I am facing :(.

                  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