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. Help with sender() analysis
Forum Updated to NodeBB v4.3 + New Features

Help with sender() analysis

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 640 Views 3 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
    Anonymous_Banned275
    wrote on last edited by
    #1

    I am trying to use sender() to find the actual "index" which triggered the connect.
    I looked at the "RX_object" and see lot of info there but cannot find "index"..
    The parameters pased to SLOT are max values , not actual "index".
    Many thanks for help.

    connect(subMenu_checkableActionInMenu[index] ,
            &QAction::triggered, this ,[=]() { this->processAction(index_sub,index) ;});
    
    QObject  *RX_object = sender();
    
    Axel SpoerlA 1 Reply Last reply
    0
    • A Anonymous_Banned275

      I am trying to use sender() to find the actual "index" which triggered the connect.
      I looked at the "RX_object" and see lot of info there but cannot find "index"..
      The parameters pased to SLOT are max values , not actual "index".
      Many thanks for help.

      connect(subMenu_checkableActionInMenu[index] ,
              &QAction::triggered, this ,[=]() { this->processAction(index_sub,index) ;});
      
      QObject  *RX_object = sender();
      
      Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      @AnneRanch
      You won't find the index there.
      sender()is a QObject, which in your case is either a QMenuor a QAction.
      If the sender of a signal needs to know certain properties, you can use setProperty, to store information and read it in the slot, see here.

      Software Engineer
      The Qt Company, Oslo

      A 1 Reply Last reply
      2
      • Axel SpoerlA Axel Spoerl

        @AnneRanch
        You won't find the index there.
        sender()is a QObject, which in your case is either a QMenuor a QAction.
        If the sender of a signal needs to know certain properties, you can use setProperty, to store information and read it in the slot, see here.

        A Offline
        A Offline
        Anonymous_Banned275
        wrote on last edited by Anonymous_Banned275
        #3

        @Axel-Spoerl per doc

        Advanced Signals and Slots Usage

        For cases where you may require information on the sender of the signal, Qt provides the QObject::sender() function, which returns a pointer to the object that sent the signal.

        so
        how do I get that pointer ?
        QObject *RX_object = sender(); ?

        and when I get it should it contain the "index" ?

        Pl45m4P JonBJ 2 Replies Last reply
        0
        • A Anonymous_Banned275

          @Axel-Spoerl per doc

          Advanced Signals and Slots Usage

          For cases where you may require information on the sender of the signal, Qt provides the QObject::sender() function, which returns a pointer to the object that sent the signal.

          so
          how do I get that pointer ?
          QObject *RX_object = sender(); ?

          and when I get it should it contain the "index" ?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @AnneRanch said in Help with sender() analysis:

          and when I get it should it contain the "index" ?

          No, it will never contain your index, because you added the structure of QMenu- and QAction-items. The single QActions don't know about any index or what you've built around, so does their base class QObject not know.
          The index of the signal emitting widget in your container is neither transmitted nor can be somehow extracted from the sender() QObject.

          If you need the container index of your sender item in your slot, there is, for sure, a better solution to do what you want to do.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • A Anonymous_Banned275

            @Axel-Spoerl per doc

            Advanced Signals and Slots Usage

            For cases where you may require information on the sender of the signal, Qt provides the QObject::sender() function, which returns a pointer to the object that sent the signal.

            so
            how do I get that pointer ?
            QObject *RX_object = sender(); ?

            and when I get it should it contain the "index" ?

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

            @AnneRanch
            sender() will in no way help you, and should be avoided.

            There are just two ways to pass values (like index_sub or index) from where you do the connect() to a slot connected to the signal:

            • Write the slot as a lambda, as we have discussed, just as your
            connect(subMenu_checkableActionInMenu[index] ,
                    &QAction::triggered, this ,[=]() { this->processAction(index_sub,index) ;});
            

            This, for example, passes the index as a parameter which is used in subMenu_checkableActionInMenu[index] that is the sender. So it is just what you are attemtping to get out of sender(). If you do not think that has the right value then there is something mixed up.

            • Add the value as a property to the sending object, as @Axel-Spoerl wrote earlier, and then use sender() to get that object and read the stored property in it. Which is a long and complicated way round compared to using a lambda.
            A 1 Reply Last reply
            0
            • JonBJ JonB

              @AnneRanch
              sender() will in no way help you, and should be avoided.

              There are just two ways to pass values (like index_sub or index) from where you do the connect() to a slot connected to the signal:

              • Write the slot as a lambda, as we have discussed, just as your
              connect(subMenu_checkableActionInMenu[index] ,
                      &QAction::triggered, this ,[=]() { this->processAction(index_sub,index) ;});
              

              This, for example, passes the index as a parameter which is used in subMenu_checkableActionInMenu[index] that is the sender. So it is just what you are attemtping to get out of sender(). If you do not think that has the right value then there is something mixed up.

              • Add the value as a property to the sending object, as @Axel-Spoerl wrote earlier, and then use sender() to get that object and read the stored property in it. Which is a long and complicated way round compared to using a lambda.
              A Offline
              A Offline
              Anonymous_Banned275
              wrote on last edited by
              #6

              @JonB I do not think I said this - but the "index
              passed is the index to LAST item in menu, NOT the current index which trigged the connect.
              Yes , I must have something in code which is wrong , obviously ... I'll work on that.

              JonBJ 1 Reply Last reply
              0
              • A Anonymous_Banned275

                @JonB I do not think I said this - but the "index
                passed is the index to LAST item in menu, NOT the current index which trigged the connect.
                Yes , I must have something in code which is wrong , obviously ... I'll work on that.

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

                @AnneRanch
                All I can say is if you try something like:

                // assume array has >= 3 elements
                for (int index = 0; index < 3; index++)
                    connect(subMenu_checkableActionInMenu[index], &QAction::triggered,
                            this, [=]() { qDebug() << index; } );
                

                I would expect you to see 0/1/2 according as which one you click on. You might like to test that.

                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