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. Call method in "parent" window from child window.
QtWS25 Last Chance

Call method in "parent" window from child window.

Scheduled Pinned Locked Moved General and Desktop
17 Posts 5 Posters 18.4k 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.
  • M Offline
    M Offline
    maxmotor
    wrote on last edited by
    #1

    Hello Qt devs!

    I'm working on a Qt application which have multiple windows. With this I mean that from my main/parent window I open a new child window. From here I make some changes to a value, which also should be updated in my main/parent window, so when I close my child window, the value is already updated in the parent window. My question is then: How do I make this method call?

    I have tried using signals and slots:

    @connect(ui.button, SIGNAL(clicked()), this->parent(),
    SLOT(updateValue()));@

    The updateValue() method is placed in my parent window class.

    When I say parent and child, it is not something I have set up in any way. I only create a new instance of my child window from my parent. is this the right way to do it?

    Thank you for your time!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      You will have to explicitly set your parent widget as a parent for your child widget, by either passing it as parent to the constructor or using QWidget::setParent().

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rahul Das
        wrote on last edited by
        #3

        Connect the child from within the parent is also possible. When some thing to be done, child emits a signal and parent catches and doing some work!

        How exactly are you creating a new window ? Using a QDialog or QWidget with out parent ?


        Declaration of (Platform) independence.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maxmotor
          wrote on last edited by
          #4

          My windows are sublcasses of QWidget.

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

            How about:

            You give your child window a signal valueChanged(int theValue);

            You give your parent window a slot (either public or private, whatever you want) changeValue(int)

            At the place where you create the child window (that is: in the parent window), you create a signal-slot connection between the above two.

            In your child window, make sure you emit the valueChanged(int) signal whenever you need.

            Do not try to make the connection directly between a widget on a child window or widget and some internal slot. It is very bad design. Instead, make sure that each part of your application has a good API, and use that.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxmotor
              wrote on last edited by
              #6

              Thank you for your replies!

              I will try your suggestion Andre.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                maxmotor
                wrote on last edited by
                #7

                I'm not sure if I'm doing this correct?

                @Setup_selectMachine *selectMachine = new Setup_selectMachine();
                QObject::connect(selectMachine, SIGNAL(valueChanged()), this, SLOT(updateValue()));
                selectMachine->show();@

                The above code is where I declare, initialize and show my child window. I've placed the signal-slot connection there as well. "selectMachine" is the name of my child object. "valueChanged()" is the signal in "selectMachine" which should trigger the change. "this" of course refers to this class. "updateValue()" is the slot in this class which I want to be triggered. I am not sending any values directly via the connection, but instead I'm using a resource class which holds all values.

                So - Am I doing it the right way?

                Thank you for your time!

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rahul Das
                  wrote on last edited by
                  #8

                  Well, looks like it is :).. Why dont you give it a try ?


                  Declaration of (Platform) independence.

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

                    Looks like a good start, if you actually created that signal and that slot.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      maxmotor
                      wrote on last edited by
                      #10

                      Hm - Nothing seems to happen I'm afraid.

                      I should mention that my slot method is completely empty. I just use it as a trigger.

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

                        A slot is not a trigger. A signal is (sort of). The slot should actually do something.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          maxmotor
                          wrote on last edited by
                          #12

                          Oh my mistake. I meant signal. My signal is only a trigger :) Sorry

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

                            You do know that you are only supposed to give a declaration of a signal, right? No implementation allowed!

                            So, only do:
                            @
                            //MyChildWindow.h
                            class MyChildWindow: public QDialog
                            {
                            Q_OBJECT
                            /...
                            signals:
                            void valueChanged(int);
                            @

                            Moc will provide an implementation for you.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              maxmotor
                              wrote on last edited by
                              #14

                              Thank you!

                              2 things:

                              1. I did make an empty implementation. Deleted it.
                              2. I forgot to declare it a signal in my header file. ("signals:")

                              I now seems to be working. The variable is not updated properly, but I guess that's due to an error somewhere else.

                              Thank you so much for your time!

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                KA51O
                                wrote on last edited by
                                #15

                                [quote author="maxmotor" date="1316523853"]I'm not sure if I'm doing this correct?

                                @Setup_selectMachine *selectMachine = new Setup_selectMachine();
                                QObject::connect(selectMachine, SIGNAL(valueChanged()), this, SLOT(updateValue()));
                                selectMachine->show();@[/quote]

                                You forgot to specify the signal and slot parameters types (int).
                                @ @Setup_selectMachine *selectMachine = new Setup_selectMachine();
                                QObject::connect(selectMachine, SIGNAL(valueChanged(int)), this, SLOT(updateValue(int)));
                                selectMachine->show(); @

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  maxmotor
                                  wrote on last edited by
                                  #16

                                  No I did not - to quote myself:

                                  [quote author="maxmotor" date="1316523853"]I am not sending any values directly via the connection, but instead I'm using a resource class which holds all values.![/quote]

                                  So I don't have to send any parameters with the signal.

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    KA51O
                                    wrote on last edited by
                                    #17

                                    Sry must have missed that part.

                                    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