Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How do I safely call methods from another object?
Forum Updated to NodeBB v4.3 + New Features

How do I safely call methods from another object?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
21 Posts 4 Posters 5.1k 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.
  • J J.Hilk
    26 Jul 2018, 05:51

    @JohnFrom
    I'm making use of @aha_1980 link collection thread

    and point you to the wiki page of the new signal sliot syntax, should give you everything you need to successfully connect to objects/classes:
    https://wiki.qt.io/New_Signal_Slot_Syntax

    and here the example form the docu:
    http://doc.qt.io/qt-5/signalsandslots.html#a-small-example

    J Offline
    J Offline
    JohnFrom
    wrote on 26 Jul 2018, 06:53 last edited by
    #12

    @J.Hilk

    Thanks a whole bunch. This is how far I managed to get:

    connect(&sender, SIGNAL(newNodeAdded()), this, SLOT(addNewNodeBySignal()));

    where:

    sender is the signal sender object.

    newNodeAdded() is the void function declared after signal: in the relevant header file.

    "this" is the signal receiver, the object that's resident as I mentioned above.

    addNewNodeBySignal is the SLOT defined in the receiver's header file.

    Now I'm having the "sender not decleared in the scope" error, kind of right back where I started.

    Any smart workaround?

    J 1 Reply Last reply 26 Jul 2018, 08:45
    0
    • J JohnFrom
      26 Jul 2018, 06:53

      @J.Hilk

      Thanks a whole bunch. This is how far I managed to get:

      connect(&sender, SIGNAL(newNodeAdded()), this, SLOT(addNewNodeBySignal()));

      where:

      sender is the signal sender object.

      newNodeAdded() is the void function declared after signal: in the relevant header file.

      "this" is the signal receiver, the object that's resident as I mentioned above.

      addNewNodeBySignal is the SLOT defined in the receiver's header file.

      Now I'm having the "sender not decleared in the scope" error, kind of right back where I started.

      Any smart workaround?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Jul 2018, 08:45 last edited by
      #13

      @JohnFrom Can you show your code?
      You did not define sender in the context where you call connect.

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

      J 1 Reply Last reply 27 Jul 2018, 01:42
      2
      • J jsulm
        26 Jul 2018, 08:45

        @JohnFrom Can you show your code?
        You did not define sender in the context where you call connect.

        J Offline
        J Offline
        JohnFrom
        wrote on 27 Jul 2018, 01:42 last edited by
        #14

        @jsulm You are absolutely correct. I knew what it meant. I just don't know the right way to solve it, other than static pointers or objects, both sound like colossal bad ideas.

        Code-wise, I'll paste some here but it would be best if I provide some background:

        First of all the sender is purposed to be a service, so I'm not sure it's the best of ideas to have it inherit Qobject and become a sender, what I'm trying to say is maybe the better way is to define a dedicated object inside this service, with its soul purpose being communicating with the outside world. I'm not sure if that's a good idea either.

        The receiver on the other hand, is a typical Qobject, responsible for presenting the UI. So naturally it has tons of signals and slots.

        And without further ado, here is the code:
        Sender.h

        class Sender: public someService, public QObject
        {
        
             Q_OBJECT
        
        signals:
            void newNodeAdded();
        
        };
        

        Sender.cpp

        ```
        
        if( !node ){
                node = new node(nodeId, nodeId);
                TnodeStore.insertnode(node);
                TDatabase.addnode(node);
                emit newnodeAdded();
            }
        
        
        Receiver.h:
        
        
        

        class nodeView : public QWidget
        {
        Q_OBJECT

        private slots:

        void onRefreshnodeClicked(void);
        };

        
        
        Receiver.cpp:
        
        

        connect((QObject*)sender, SIGNAL(newNodeAdded()), this, SLOT(onRefreshnodeClicked()));

        
        
        
        Note this code was updated with some errors rectified.
        J 1 Reply Last reply 27 Jul 2018, 04:12
        0
        • J JohnFrom
          27 Jul 2018, 01:42

          @jsulm You are absolutely correct. I knew what it meant. I just don't know the right way to solve it, other than static pointers or objects, both sound like colossal bad ideas.

          Code-wise, I'll paste some here but it would be best if I provide some background:

          First of all the sender is purposed to be a service, so I'm not sure it's the best of ideas to have it inherit Qobject and become a sender, what I'm trying to say is maybe the better way is to define a dedicated object inside this service, with its soul purpose being communicating with the outside world. I'm not sure if that's a good idea either.

          The receiver on the other hand, is a typical Qobject, responsible for presenting the UI. So naturally it has tons of signals and slots.

          And without further ado, here is the code:
          Sender.h

          class Sender: public someService, public QObject
          {
          
               Q_OBJECT
          
          signals:
              void newNodeAdded();
          
          };
          

          Sender.cpp

          ```
          
          if( !node ){
                  node = new node(nodeId, nodeId);
                  TnodeStore.insertnode(node);
                  TDatabase.addnode(node);
                  emit newnodeAdded();
              }
          
          
          Receiver.h:
          
          
          

          class nodeView : public QWidget
          {
          Q_OBJECT

          private slots:

          void onRefreshnodeClicked(void);
          };

          
          
          Receiver.cpp:
          
          

          connect((QObject*)sender, SIGNAL(newNodeAdded()), this, SLOT(onRefreshnodeClicked()));

          
          
          
          Note this code was updated with some errors rectified.
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 27 Jul 2018, 04:12 last edited by
          #15

          @JohnFrom said in How do I safely call methods from another object?:

          sender

          where is this "sender" defined?
          The code you posted does not help much as it is not complete.

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

          J 1 Reply Last reply 27 Jul 2018, 06:32
          0
          • J jsulm
            27 Jul 2018, 04:12

            @JohnFrom said in How do I safely call methods from another object?:

            sender

            where is this "sender" defined?
            The code you posted does not help much as it is not complete.

            J Offline
            J Offline
            JohnFrom
            wrote on 27 Jul 2018, 06:32 last edited by JohnFrom
            #16

            @jsulm

            Sorry!

            The other parts of the code are really irrelevant and I don't want to have anything to do with the NDA so I'll have to pretty much manually quasi-obfuscate the code which is a lot of work.

            "sender" is defined just like what you read, in Sender.cpp and Sender.h

            I assume you were trying to say where it was instantiated? It's instantiated in a rather unconventional way:

            SenderService *sender = NULL;
                if( condition == A ){
                    sender = new senderServiceA();
                }else if( condition == B ){
                    sender = new senderServiceB();
                }else{
                    qDebug() << "Invalid condition!";
                }
            

            Note that both senderServiceA and senderServiceB are "child classes" of SenderService.

            That code above was instantiated as an object in

            MainWindow class, which is a "child classes" of QMainWindow, and will be executed during run time.

            However it is quite impossible for the UI (that persistent object I mentioned above, lives as long as the program is running) to access this service object. Shall I write another object dedicated in interconnecting the 2 objects as some sort of proxy, or shall I embed this "communication object" inside one of the 2 objects?

            Or I should stick with basics, try to enable both objects to use slots? I'm reluctant to go down that route because I don't want either object to be able to directly access another and strangely enough, for object B to receive signal from object A, object A must be defined in the scope visible to B.

            J J 2 Replies Last reply 27 Jul 2018, 06:44
            0
            • J JohnFrom
              27 Jul 2018, 06:32

              @jsulm

              Sorry!

              The other parts of the code are really irrelevant and I don't want to have anything to do with the NDA so I'll have to pretty much manually quasi-obfuscate the code which is a lot of work.

              "sender" is defined just like what you read, in Sender.cpp and Sender.h

              I assume you were trying to say where it was instantiated? It's instantiated in a rather unconventional way:

              SenderService *sender = NULL;
                  if( condition == A ){
                      sender = new senderServiceA();
                  }else if( condition == B ){
                      sender = new senderServiceB();
                  }else{
                      qDebug() << "Invalid condition!";
                  }
              

              Note that both senderServiceA and senderServiceB are "child classes" of SenderService.

              That code above was instantiated as an object in

              MainWindow class, which is a "child classes" of QMainWindow, and will be executed during run time.

              However it is quite impossible for the UI (that persistent object I mentioned above, lives as long as the program is running) to access this service object. Shall I write another object dedicated in interconnecting the 2 objects as some sort of proxy, or shall I embed this "communication object" inside one of the 2 objects?

              Or I should stick with basics, try to enable both objects to use slots? I'm reluctant to go down that route because I don't want either object to be able to directly access another and strangely enough, for object B to receive signal from object A, object A must be defined in the scope visible to B.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 27 Jul 2018, 06:44 last edited by
              #17

              @JohnFrom said in How do I safely call methods from another object?:

              SenderService *sender = NULL;

              sender here is a LOCAL variable!
              Do you call connect in same scope?
              Posting such small pieces of code makes it hard for others to understand what you do and where the problem is...

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

              1 Reply Last reply
              1
              • J JohnFrom
                27 Jul 2018, 06:32

                @jsulm

                Sorry!

                The other parts of the code are really irrelevant and I don't want to have anything to do with the NDA so I'll have to pretty much manually quasi-obfuscate the code which is a lot of work.

                "sender" is defined just like what you read, in Sender.cpp and Sender.h

                I assume you were trying to say where it was instantiated? It's instantiated in a rather unconventional way:

                SenderService *sender = NULL;
                    if( condition == A ){
                        sender = new senderServiceA();
                    }else if( condition == B ){
                        sender = new senderServiceB();
                    }else{
                        qDebug() << "Invalid condition!";
                    }
                

                Note that both senderServiceA and senderServiceB are "child classes" of SenderService.

                That code above was instantiated as an object in

                MainWindow class, which is a "child classes" of QMainWindow, and will be executed during run time.

                However it is quite impossible for the UI (that persistent object I mentioned above, lives as long as the program is running) to access this service object. Shall I write another object dedicated in interconnecting the 2 objects as some sort of proxy, or shall I embed this "communication object" inside one of the 2 objects?

                Or I should stick with basics, try to enable both objects to use slots? I'm reluctant to go down that route because I don't want either object to be able to directly access another and strangely enough, for object B to receive signal from object A, object A must be defined in the scope visible to B.

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 27 Jul 2018, 06:57 last edited by J.Hilk
                #18

                @JohnFrom said in How do I safely call methods from another object?:

                Or I should stick with basics, try to enable both objects to use slots? I'm reluctant to go down that route because I don't want either object to be able to directly access another and strangely enough, for object B to receive signal from object A, object A must be defined in the scope visible to B.

                whats that supposed to mean?

                For QObject::connect to work, at least the Sender-object needs to have QObject as its base class, sender and receiver, if you're using qt4 syntax.

                Also Signal & Slot connections do not violate c++ standards/rules, you can connect only to functions and/or slots outside the scope where connect is called, that are public.

                if object A lives in object C, and object B lives in object C, than you can use QObject::connect in object C to connect A & B without A knowing about B or visa versa.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                J 1 Reply Last reply 27 Jul 2018, 07:03
                0
                • J J.Hilk
                  27 Jul 2018, 06:57

                  @JohnFrom said in How do I safely call methods from another object?:

                  Or I should stick with basics, try to enable both objects to use slots? I'm reluctant to go down that route because I don't want either object to be able to directly access another and strangely enough, for object B to receive signal from object A, object A must be defined in the scope visible to B.

                  whats that supposed to mean?

                  For QObject::connect to work, at least the Sender-object needs to have QObject as its base class, sender and receiver, if you're using qt4 syntax.

                  Also Signal & Slot connections do not violate c++ standards/rules, you can connect only to functions and/or slots outside the scope where connect is called, that are public.

                  if object A lives in object C, and object B lives in object C, than you can use QObject::connect in object C to connect A & B without A knowing about B or visa versa.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 27 Jul 2018, 07:03 last edited by
                  #19

                  @J.Hilk I think the problem is that he declares sender als local variable and when he tries to connect "sender" is undefined. This kind of mistake is one of the most common here :-)

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

                  J 1 Reply Last reply 27 Jul 2018, 07:10
                  1
                  • J jsulm
                    27 Jul 2018, 07:03

                    @J.Hilk I think the problem is that he declares sender als local variable and when he tries to connect "sender" is undefined. This kind of mistake is one of the most common here :-)

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 27 Jul 2018, 07:10 last edited by
                    #20

                    @jsulm possible, we simply don't know enough of the actual code to say for sure.

                    However, am I wrong here, or is "xxxx not decleared in the scope error " not usually a sign of missing includes ?
                    I tried to use connect on a null pointer recently.
                    It compiled, it run, it did not crash, I only got a warning " cannot connect to nullptr".

                    More infos needed!


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    J 1 Reply Last reply 27 Jul 2018, 07:19
                    0
                    • J J.Hilk
                      27 Jul 2018, 07:10

                      @jsulm possible, we simply don't know enough of the actual code to say for sure.

                      However, am I wrong here, or is "xxxx not decleared in the scope error " not usually a sign of missing includes ?
                      I tried to use connect on a null pointer recently.
                      It compiled, it run, it did not crash, I only got a warning " cannot connect to nullptr".

                      More infos needed!

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 27 Jul 2018, 07:19 last edited by
                      #21

                      @J.Hilk

                      connect((QObject*)sender, SIGNAL(newNodeAdded()), this, SLOT(onRefreshnodeClicked()));
                      

                      "sender not decleared in the scope"

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

                      1 Reply Last reply
                      0

                      21/21

                      27 Jul 2018, 07:19

                      • Login

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