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. slot is called multiple times when signal is emitted
Forum Updated to NodeBB v4.3 + New Features

slot is called multiple times when signal is emitted

Scheduled Pinned Locked Moved Unsolved General and Desktop
signal & slot
11 Posts 4 Posters 7.2k 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #2

    @NIXIN said:

    connect(this, SIGNAL(sig()), obj, SLOT(slt()));

    Do the following.
    connect(this, SIGNAL(sig()), obj, SLOT(slt()),Qt::UniqueConnection);

    It will fix your issue.

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    NIXINN 1 Reply Last reply
    4
    • NIXINN NIXIN

      Consider functions like below:

      void class1::function1()
      {
      class2 *obj = new class2();
      connect(this, SIGNAL(sig()), obj, SLOT(slt()));
      }

      void class2::function2()
      {
      emit sig();
      }
      I am invoking function1() multiple times. function2() is also triggered multiple times

      What I want is, whenever sig() is emitted slt() should be called, but what's happening is, first time when sig() is emitted slt() is being called number of times function1() is invoked. Next time when sig() is invoked slt() is not called. It would be a great help, if somebody can help me achieve this.....

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #3

      @NIXIN
      in your function1() you creating objects on the heap and connect their signals to some of your slots. So the slot is called for each connection made.
      But from your code it's not clear if you delete the objects when you don't need them anymore. If not the connection is still alive until you call disconnect or one of the objects is destroyed.

      @NIXIN said:

      Next time when sig() is invoked slt() is not called. It would be a great help, if somebody can help me achieve this.....

      Please clarify "next time". What happens in the meantime?

      --- 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

      NIXINN 1 Reply Last reply
      1
      • dheerendraD dheerendra

        @NIXIN said:

        connect(this, SIGNAL(sig()), obj, SLOT(slt()));

        Do the following.
        connect(this, SIGNAL(sig()), obj, SLOT(slt()),Qt::UniqueConnection);

        It will fix your issue.

        NIXINN Offline
        NIXINN Offline
        NIXIN
        wrote on last edited by
        #4

        @dheerendra well I have tried that, but it does not work.
        I think Qt::UniqueConnection works when same object is used multiple times but here every time when function1() is invoked a new object of class2 is created

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #5

          I did not observe. That is the issue. Why are you creating the new object overtime. Since you are creating the object on the heap, old object is not deleted. So old object still responds for signal. Create the object in constructor and use it everywhere.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          4
          • raven-worxR raven-worx

            @NIXIN
            in your function1() you creating objects on the heap and connect their signals to some of your slots. So the slot is called for each connection made.
            But from your code it's not clear if you delete the objects when you don't need them anymore. If not the connection is still alive until you call disconnect or one of the objects is destroyed.

            @NIXIN said:

            Next time when sig() is invoked slt() is not called. It would be a great help, if somebody can help me achieve this.....

            Please clarify "next time". What happens in the meantime?

            NIXINN Offline
            NIXINN Offline
            NIXIN
            wrote on last edited by
            #6

            @raven-worx yes you are right, in the slot I am deleting the object. why I am doing this is because class2 is actually a tab and when sig() is emitted I want to close that tab.
            In function1() I am creating this tab which is taking some files. That's why whenever I have to read a file I have to call function1().
            Please help me to solve the problem....

            raven-worxR 1 Reply Last reply
            0
            • NIXINN NIXIN

              @raven-worx yes you are right, in the slot I am deleting the object. why I am doing this is because class2 is actually a tab and when sig() is emitted I want to close that tab.
              In function1() I am creating this tab which is taking some files. That's why whenever I have to read a file I have to call function1().
              Please help me to solve the problem....

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #7

              @NIXIN
              this doesn't seem to be correct no?

              class2 *obj = new class2();
              connect(this, SIGNAL(sig()), obj, SLOT(slt()));
              

              The sender should be "obj" instead of "this"?!

              But you should already see a warning message (that the connect() failed becasue the signal isn't available on this class) in the console if this is the case...

              --- 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

              NIXINN 1 Reply Last reply
              1
              • raven-worxR raven-worx

                @NIXIN
                this doesn't seem to be correct no?

                class2 *obj = new class2();
                connect(this, SIGNAL(sig()), obj, SLOT(slt()));
                

                The sender should be "obj" instead of "this"?!

                But you should already see a warning message (that the connect() failed becasue the signal isn't available on this class) in the console if this is the case...

                NIXINN Offline
                NIXINN Offline
                NIXIN
                wrote on last edited by
                #8

                @raven-worx Yes, again u r right
                You can consider something like this:

                class1::class1()
                {
                class3 ob = new class3(this);
                }

                void class1::function1()
                {
                class2 *obj = new class2();
                connect(ob, SIGNAL(sig()), obj, SLOT(slt()));
                }

                void class3::function2()
                {
                emit sig();
                }

                raven-worxR 1 Reply Last reply
                0
                • NIXINN NIXIN

                  @raven-worx Yes, again u r right
                  You can consider something like this:

                  class1::class1()
                  {
                  class3 ob = new class3(this);
                  }

                  void class1::function1()
                  {
                  class2 *obj = new class2();
                  connect(ob, SIGNAL(sig()), obj, SLOT(slt()));
                  }

                  void class3::function2()
                  {
                  emit sig();
                  }

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #9

                  @NIXIN
                  well...if you still expect some help how about to post your actual code?
                  Instead of some pseudo code (which only contains lines which YOU think are relevant) which probably isn't even correct... i am tired of guessing.

                  Again...is a connection on itself really what you want?!

                  class2 *obj = new class2();
                  connect(obj, SIGNAL(sig()), obj, SLOT(slt()));
                  

                  --- 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

                  NIXINN 1 Reply Last reply
                  0
                  • raven-worxR raven-worx

                    @NIXIN
                    well...if you still expect some help how about to post your actual code?
                    Instead of some pseudo code (which only contains lines which YOU think are relevant) which probably isn't even correct... i am tired of guessing.

                    Again...is a connection on itself really what you want?!

                    class2 *obj = new class2();
                    connect(obj, SIGNAL(sig()), obj, SLOT(slt()));
                    
                    NIXINN Offline
                    NIXINN Offline
                    NIXIN
                    wrote on last edited by
                    #10

                    @raven-worx No, I want a connection between objects of class2 and class3

                    1 Reply Last reply
                    0
                    • Pradeep KumarP Offline
                      Pradeep KumarP Offline
                      Pradeep Kumar
                      wrote on last edited by
                      #11

                      if u want to establish a connectin between the objects2 and objects3,

                      as u said earlier u want to close te tab of class 2

                      class2::class2{

                      // create the object of class 3 here in constructor.

                      Class3 obj3* = new Class3;

                      connect(obj3,SIGNAL(signal()),this,SLOT(close()),Qt::UniqueConnection);

                      }

                      class3::class3
                      {
                      }

                      class3::function()
                      {

                      emit signal();
                      }

                      so the object will be created in constructor of Class2.
                      as the signal is emmitted , and when it is triggered the class2 will be closed. as per the post which u want to close the tab when signal is emmitted.

                      Pradeep Kumar
                      Qt,QML Developer

                      1 Reply Last reply
                      1

                      • Login

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