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. Example of calling a function to parent?
Forum Updated to NodeBB v4.3 + New Features

Example of calling a function to parent?

Scheduled Pinned Locked Moved Unsolved General and Desktop
38 Posts 5 Posters 4.8k Views 2 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.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by Panoss
    #14

    And this is the function in my FormB: ()

    void FormB::setValue(int value)
    {       
            emit valueChanged(value);   
    }
    

    Correct?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Panoss
      wrote on last edited by Panoss
      #15

      And now this:

      QObject::connect(&a, &Counter::valueChanged,
                           &b, &Counter::setValue);
      

      I modified it to (this code is in the constructor of FormB):

      connect(&parent, &parent::valueChanged,
                               this, &FormB::setValue);
      

      ???
      It's wrong...The 'parent' I mean.
      The sender is FormB...so..

      M 1 Reply Last reply
      0
      • P Panoss

        And now this:

        QObject::connect(&a, &Counter::valueChanged,
                             &b, &Counter::setValue);
        

        I modified it to (this code is in the constructor of FormB):

        connect(&parent, &parent::valueChanged,
                                 this, &FormB::setValue);
        

        ???
        It's wrong...The 'parent' I mean.
        The sender is FormB...so..

        M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #16

        @Panoss
        Everything is upside down :)

        let's recap, you want the main window (formA) to be inform when a value has changed in second window (formB)

        The main window register to receive a signal from the second window.

        In formA:

        connect(formB, &FormB::valueChanged, this , &FormA::valueChangedInFormB);
        
        1 Reply Last reply
        3
        • P Offline
          P Offline
          Panoss
          wrote on last edited by Panoss
          #17

          @mpergand it works!!!
          Thank you all!

          The actual name of FormA is 'ArticlesWindow' and of FormB 'positionsForm'.
          (FormB is the sender)
          I post the code in case someone needs it:

          articleswindow.h
          private slots:    
              void positionChanged();
          
          articleswindow.cpp
          void ArticlesWindow::positionChanged(){
              qDebug() << "ArticlesWindow::positionChanged";
          }
          void ArticlesWindow::openPositionsForm(){    
              class positionsForm *form = new class positionsForm(model, this);
              connect(form, &positionsForm::valueChanged, this , &ArticlesWindow::positionChanged);
          
          

          And:

          positionsform.h
            public slots:
              void setValue(int value);
          
            signals:
              void valueChanged(int newValue);
          
          positionsform.cpp
           void positionsForm::setValue(int value)
           {          
             emit valueChanged(value);    
           }
          
          
          P 1 Reply Last reply
          0
          • P Panoss

            @mpergand it works!!!
            Thank you all!

            The actual name of FormA is 'ArticlesWindow' and of FormB 'positionsForm'.
            (FormB is the sender)
            I post the code in case someone needs it:

            articleswindow.h
            private slots:    
                void positionChanged();
            
            articleswindow.cpp
            void ArticlesWindow::positionChanged(){
                qDebug() << "ArticlesWindow::positionChanged";
            }
            void ArticlesWindow::openPositionsForm(){    
                class positionsForm *form = new class positionsForm(model, this);
                connect(form, &positionsForm::valueChanged, this , &ArticlesWindow::positionChanged);
            
            

            And:

            positionsform.h
              public slots:
                void setValue(int value);
            
              signals:
                void valueChanged(int newValue);
            
            positionsform.cpp
             void positionsForm::setValue(int value)
             {          
               emit valueChanged(value);    
             }
            
            
            P Offline
            P Offline
            Panoss
            wrote on last edited by
            #18

            It works but the message is printed twice, like if function ArticlesWindow::positionChanged is called twice??

            M 1 Reply Last reply
            0
            • P Panoss

              It works but the message is printed twice, like if function ArticlesWindow::positionChanged is called twice??

              M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #19

              @Panoss

              class positionsForm *form = new class positionsForm(model, this);

              What is that ?

              You seem to create an new instance each time openPositionsForm() is called ...

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Panoss
                wrote on last edited by Panoss
                #20

                Why is this wrong?
                Every time I want to open the form I create an instance of it 's class.
                When I close the form the instance is destroyed, right?

                What 's the correct way?

                M jsulmJ JonBJ 3 Replies Last reply
                0
                • P Panoss

                  Why is this wrong?
                  Every time I want to open the form I create an instance of it 's class.
                  When I close the form the instance is destroyed, right?

                  What 's the correct way?

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #21

                  @Panoss
                  Add sender() and look if it's different objects
                  qDebug() << "ArticlesWindow::positionChanged"<<sender();

                  P 1 Reply Last reply
                  0
                  • P Panoss

                    Why is this wrong?
                    Every time I want to open the form I create an instance of it 's class.
                    When I close the form the instance is destroyed, right?

                    What 's the correct way?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #22

                    @Panoss said in Example of calling a function to parent?:

                    Every time I want to open the form I create an instance of it 's class.

                    Do you also delete the instance when you do not need it anymore?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M P 2 Replies Last reply
                    1
                    • jsulmJ jsulm

                      @Panoss said in Example of calling a function to parent?:

                      Every time I want to open the form I create an instance of it 's class.

                      Do you also delete the instance when you do not need it anymore?

                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by mpergand
                      #23

                      @jsulm said in Example of calling a function to parent?:

                      @Panoss said in Example of calling a function to parent?:

                      Every time I want to open the form I create an instance of it 's class.

                      Do you also delete the instance when you do not need it anymore?

                      That's what @Panoss should find out for himself by looking at the sender() on multiple messages.

                      1 Reply Last reply
                      0
                      • M mpergand

                        @Panoss
                        Add sender() and look if it's different objects
                        qDebug() << "ArticlesWindow::positionChanged"<<sender();

                        P Offline
                        P Offline
                        Panoss
                        wrote on last edited by
                        #24

                        @mpergand said in Example of calling a function to parent?:

                        @Panoss
                        Add sender() and look if it's different objects
                        qDebug() << "ArticlesWindow::positionChanged"<<sender();

                        This is the output:

                        updateParent called!!
                        ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
                        updateParent called!!
                        ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
                        
                        1 Reply Last reply
                        0
                        • P Panoss

                          Why is this wrong?
                          Every time I want to open the form I create an instance of it 's class.
                          When I close the form the instance is destroyed, right?

                          What 's the correct way?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #25

                          @Panoss said in Example of calling a function to parent?:

                          When I close the form the instance is destroyed, right?

                          No, that depends on what you mean/do by "close". And we don't see that in your shown code?

                          It works but the message is printed twice, like if function ArticlesWindow::positionChanged is called twice??

                          Does the "twice" happen from the very first time you create the form or after a second open? Are the number calls equal to the number of times you create the form? If it stays at "twice" always, that alters what i would look for in the code.

                          P 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @Panoss said in Example of calling a function to parent?:

                            Every time I want to open the form I create an instance of it 's class.

                            Do you also delete the instance when you do not need it anymore?

                            P Offline
                            P Offline
                            Panoss
                            wrote on last edited by
                            #26

                            @jsulm said in Example of calling a function to parent?:

                            @Panoss said in Example of calling a function to parent?:

                            Every time I want to open the form I create an instance of it 's class.

                            Do you also delete the instance when you do not need it anymore?

                            When I close the form instance (clicking on the close button or with form->close) doesn't it get deleted?

                            e.g. with this code:

                            void addArticle::on_cancelButton_clicked()
                            {
                                this->close();
                            }
                            
                            JonBJ 1 Reply Last reply
                            0
                            • P Panoss

                              @jsulm said in Example of calling a function to parent?:

                              @Panoss said in Example of calling a function to parent?:

                              Every time I want to open the form I create an instance of it 's class.

                              Do you also delete the instance when you do not need it anymore?

                              When I close the form instance (clicking on the close button or with form->close) doesn't it get deleted?

                              e.g. with this code:

                              void addArticle::on_cancelButton_clicked()
                              {
                                  this->close();
                              }
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #27

                              @Panoss
                              Only if you have the "delete on close" attribute set on the window, I can't recall right now its name. [UPDATE: Qt::WA_DeleteOnClose].

                              But I would know (I think) if you answered:

                              Does the "twice" happen from the very first time you create the form or after a second open? Are the number calls equal to the number of times you create the form? If it stays at "twice" always, that alters what i would look for in the code.

                              which is why I asked that.....

                              1 Reply Last reply
                              2
                              • JonBJ JonB

                                @Panoss said in Example of calling a function to parent?:

                                When I close the form the instance is destroyed, right?

                                No, that depends on what you mean/do by "close". And we don't see that in your shown code?

                                It works but the message is printed twice, like if function ArticlesWindow::positionChanged is called twice??

                                Does the "twice" happen from the very first time you create the form or after a second open? Are the number calls equal to the number of times you create the form? If it stays at "twice" always, that alters what i would look for in the code.

                                P Offline
                                P Offline
                                Panoss
                                wrote on last edited by Panoss
                                #28

                                @JonB said in Example of calling a function to parent?:

                                @Panoss said in Example of calling a function to parent?:

                                When I close the form the instance is destroyed, right?

                                No, that depends on what you mean/do by "close". And we don't see that in your shown code?

                                It works but the message is printed twice, like if function ArticlesWindow::positionChanged is called twice??

                                Does the "twice" happen from the very first time you create the form or after a second open?

                                From the very first.

                                @JonB said in Example of calling a function to parent?:
                                Are the number calls equal to the number of times you create the form? If it stays at "twice" always, that alters what i would look for in the code.

                                You mean number of calls to the function?
                                I only call it once. Now, why it acts like if I call it twice, I have no idea!!

                                JonBJ 1 Reply Last reply
                                0
                                • P Panoss

                                  @JonB said in Example of calling a function to parent?:

                                  @Panoss said in Example of calling a function to parent?:

                                  When I close the form the instance is destroyed, right?

                                  No, that depends on what you mean/do by "close". And we don't see that in your shown code?

                                  It works but the message is printed twice, like if function ArticlesWindow::positionChanged is called twice??

                                  Does the "twice" happen from the very first time you create the form or after a second open?

                                  From the very first.

                                  @JonB said in Example of calling a function to parent?:
                                  Are the number calls equal to the number of times you create the form? If it stays at "twice" always, that alters what i would look for in the code.

                                  You mean number of calls to the function?
                                  I only call it once. Now, why it acts like if I call it twice, I have no idea!!

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #29

                                  @Panoss
                                  Your ArticlesWindow::openPositionsForm() goes class positionsForm *form = new class positionsForm(model, this);. I want to know how many times you call openPositionsForm(), I believe you create each time you do something like click on something. I want to know whether the number of times you call openPositionsForm() (number of times you click to open it??) is or is not equal to the number of times you report

                                  updateParent called!!
                                  ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
                                  

                                  gets output. If you open & close the "positions form" 10 times do you get 10 "updateParent called!!" output?

                                  Meanwhile, where you have emit valueChanged(value); let's have a

                                  qDebug() << "emit valueChanged(value);"  << value;
                                  
                                  P 2 Replies Last reply
                                  0
                                  • JonBJ JonB

                                    @Panoss
                                    Your ArticlesWindow::openPositionsForm() goes class positionsForm *form = new class positionsForm(model, this);. I want to know how many times you call openPositionsForm(), I believe you create each time you do something like click on something. I want to know whether the number of times you call openPositionsForm() (number of times you click to open it??) is or is not equal to the number of times you report

                                    updateParent called!!
                                    ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
                                    

                                    gets output. If you open & close the "positions form" 10 times do you get 10 "updateParent called!!" output?

                                    Meanwhile, where you have emit valueChanged(value); let's have a

                                    qDebug() << "emit valueChanged(value);"  << value;
                                    
                                    P Offline
                                    P Offline
                                    Panoss
                                    wrote on last edited by Panoss
                                    #30

                                    @JonB

                                    1st opening of the positionsForm. Edited data and updated:
                                    updateParent called!!
                                    ArticlesWindow::positionChanged positionsForm(0xc2423e7660, name = "positionsForm")
                                    updateParent called!!
                                    ArticlesWindow::positionChanged positionsForm(0xc2423e7660, name = "positionsForm")
                                    
                                    2nd  opening of the positionsForm. Edited data and updated:
                                    updateParent called!!
                                    ArticlesWindow::positionChanged positionsForm(0xc2423e3980, name = "positionsForm")
                                    updateParent called!!
                                    ArticlesWindow::positionChanged positionsForm(0xc2423e3980, name = "positionsForm")
                                    
                                    3rd  opening of the positionsForm. Edited data and updated:
                                    updateParent called!!
                                    ArticlesWindow::positionChanged positionsForm(0xc2423e86a0, name = "positionsForm")
                                    updateParent called!!
                                    ArticlesWindow::positionChanged positionsForm(0xc2423e86a0, name = "positionsForm")
                                    

                                    So no matter how many times I open (and close) the positionsForm (which, as you correctly supposed, is created each time I click on a button), the function (positionChanged) gets called twice.
                                    I 'll have to look more thoroughly in the code, something stupid I must have done somewhere.

                                    1 Reply Last reply
                                    1
                                    • JonBJ JonB

                                      @Panoss
                                      Your ArticlesWindow::openPositionsForm() goes class positionsForm *form = new class positionsForm(model, this);. I want to know how many times you call openPositionsForm(), I believe you create each time you do something like click on something. I want to know whether the number of times you call openPositionsForm() (number of times you click to open it??) is or is not equal to the number of times you report

                                      updateParent called!!
                                      ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
                                      

                                      gets output. If you open & close the "positions form" 10 times do you get 10 "updateParent called!!" output?

                                      Meanwhile, where you have emit valueChanged(value); let's have a

                                      qDebug() << "emit valueChanged(value);"  << value;
                                      
                                      P Offline
                                      P Offline
                                      Panoss
                                      wrote on last edited by
                                      #31

                                      @JonB said in Example of calling a function to parent?:

                                      @Panoss
                                      Meanwhile, where you have emit valueChanged(value); let's have a

                                      qDebug() << "emit valueChanged(value);"  << value;
                                      
                                      updateParent called!!
                                      ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm")
                                      emit valueChanged(value); 1
                                      updateParent called!!
                                      ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm")
                                      emit valueChanged(value); 1
                                      

                                      The signal is emited twice???

                                      JonBJ M 2 Replies Last reply
                                      0
                                      • P Panoss

                                        @JonB said in Example of calling a function to parent?:

                                        @Panoss
                                        Meanwhile, where you have emit valueChanged(value); let's have a

                                        qDebug() << "emit valueChanged(value);"  << value;
                                        
                                        updateParent called!!
                                        ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm")
                                        emit valueChanged(value); 1
                                        updateParent called!!
                                        ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm")
                                        emit valueChanged(value); 1
                                        

                                        The signal is emited twice???

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #32

                                        @Panoss
                                        OK to the it-only-ever-gets-called-twice. (You may still have a leak on non-destruction, we'll leave that for now.)

                                        So... just how many times does your code call positionsForm::setValue(int value)?? Could that be twice, somewhere?

                                        1 Reply Last reply
                                        0
                                        • P Panoss

                                          @JonB said in Example of calling a function to parent?:

                                          @Panoss
                                          Meanwhile, where you have emit valueChanged(value); let's have a

                                          qDebug() << "emit valueChanged(value);"  << value;
                                          
                                          updateParent called!!
                                          ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm")
                                          emit valueChanged(value); 1
                                          updateParent called!!
                                          ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm")
                                          emit valueChanged(value); 1
                                          

                                          The signal is emited twice???

                                          M Offline
                                          M Offline
                                          mpergand
                                          wrote on last edited by
                                          #33

                                          @Panoss said in Example of calling a function to parent?:

                                          @JonB said in Example of calling a function to parent?:

                                          @Panoss
                                          Meanwhile, where you have emit valueChanged(value); let's have a

                                          qDebug() << "emit valueChanged(value);"  << value;
                                          
                                          updateParent called!!
                                          ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm")
                                          emit valueChanged(value); 1
                                          updateParent called!!
                                          ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm")
                                          emit valueChanged(value); 1
                                          

                                          The signal is emited twice???

                                          Put a break point and look at the stack trace upon each call.

                                          As @JonB said, nothing is delete until you set it with:
                                          setAttribute(Qt::WA_DeleteOnClose);

                                          1 Reply Last reply
                                          2

                                          • Login

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