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. Passing a value to slot...
QtWS25 Last Chance

Passing a value to slot...

Scheduled Pinned Locked Moved Unsolved C++ Gurus
18 Posts 8 Posters 2.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.
  • S Offline
    S Offline
    sanqi
    wrote on last edited by
    #4

    Maybe you can do like this.

    QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
        QString buttonName = clickedButton->objectName();
    
    J.HilkJ 1 Reply Last reply
    0
    • S sanqi

      Maybe you can do like this.

      QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
          QString buttonName = clickedButton->objectName();
      
      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #5

      @sanqi you're of course more correct, but please don't encourage the use of bad practice :D. Namely using sender() in the first place.


      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.

      1 Reply Last reply
      0
      • Ronel_qtmasterR Offline
        Ronel_qtmasterR Offline
        Ronel_qtmaster
        wrote on last edited by
        #6

        if the signal contains bool, the slot as well have to contain a bool argument.
        Now if you want to use the index when a certain value is present, create a function apart which will take the index argument.

        An exemple

        connect(sender, SIGNAL(calling(bool)), receiver, SLOT(called(bool)));
        then,

        void MyClass:: called(bool value){

        if (value)
        {
        startIndex(index);
        }
        }

        1 Reply Last reply
        0
        • A Anonymous_Banned275

          MORE (stupid) update
          I got the connect working...

          So I know about object by sender() , but WHAT / where is sender actually identifying the "sender" of the SIGNAL ?
          My is one of the array of menus and I need to know which one "triggered" the SIGNAL...

          Here is "sender() " code - none of the lines identify the SIGNAL "sender"

            QObject* obj = sender();
            obj->dynamicPropertyNames();
            obj->dumpObjectTree();
            text = obj->objectName();
           obj->dumpObjectInfo(); // << text;
           qDebug()<< text;
          

          UPDATE
          I guess this is a wrong idea - SIGNAL / SLOT in general do not pass anything - just connect ....
          it is the SLOT which will will have to check the current index ....

          This is a follow-up on previous post.

          My task is to pass value - int to the slot.

          Here is my code :

                                  // //connect(tempmenu, &QMenu::triggered, this, &MainWindow_Bluetooth::openSerialPort);
          connect(subMenu[index] , &QMenu::triggered(bool ), this,&MainWindow_Bluetooth::testSlot(index));
          
                              }// connect block
          
          

          PLEASE HELP ME TO DECIPHER THE ERROR MESSAGES.

          I understand the SIGNAL "value " is default bool and I am trying to have SLOT with passed int ...

          I need to match them, correct ?

          /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth.cpp:3712: error: call to non-static member function without an object argument
          mainwindow_Bluetooth.cpp:3712:34: error: call to non-static member function without an object argument
          connect(subMenu[index] , &QMenu::triggered(bool ), this,&MainWindow_Bluetooth::testSlot(index));
                                    ~~~~~~~^~~~~~~~~
          

          I did try
          &QMenu::triggered(index ),

          and obviously it did not work.

          TomZT Offline
          TomZT Offline
          TomZ
          wrote on last edited by
          #7

          @AnneRanch said in Passing a value to slot...:

          I guess this is a wrong idea - SIGNAL / SLOT in general do not pass anything - just connect ....
          it is the SLOT which will will have to check the current index ....

          so, as you found out, you need to literally see it as a switchboard connection.

          The main example is when you have a slider that emits a new position, you can connect it to a second slider or a wheel to auto-follow with zero code on your side. Connections like that can even be created in QtDesigner.

          More to the point is that you can look at QMenu API docs; https://doc.qt.io/qt-6/qmenu.html#signals
          and design your code around that.

          There is still the trick of passing in a lambda in your connect which gives you more power, but you should absolutely leave that for the last option. For instance I expect if you use QAction for your menu's you'll have much less controller code to debug

          1 Reply Last reply
          0
          • Axel SpoerlA Axel Spoerl

            @AnneRanch said in Passing a value to slot...:

            &QMenu::triggered(index ),

            Wrong. I told you here, how to correctly phrase the connect statement.
            I fail to understand, why you modify it again and wonder about errors.

            The triggeredslot of QMenu passes the pointer to the triggered action, see here. I don't know why you expect it to pass a boolean. So even if the compiler would understand the broken syntax of the connect statement, the type would be wrong.

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

            @Axel-Spoerl said in Passing a value to slot...:

            The triggeredslot of QMenu passes the pointer to the triggered action, see here. I don't know why you expect it to pass a boolean. So even if the compiler would understand the broken syntax of the connect statement, the type would be wrong.

            Exactly.

            The requirement here, apart from the syntax error, is
            (a) attach to QAction::triggered signal (or maybe even QAction::toggled if it's a checkbox); and
            (b) will requite a lambda for the slot if OP wants to pass an index to identify item pressed.

            A 1 Reply Last reply
            0
            • JonBJ JonB

              @Axel-Spoerl said in Passing a value to slot...:

              The triggeredslot of QMenu passes the pointer to the triggered action, see here. I don't know why you expect it to pass a boolean. So even if the compiler would understand the broken syntax of the connect statement, the type would be wrong.

              Exactly.

              The requirement here, apart from the syntax error, is
              (a) attach to QAction::triggered signal (or maybe even QAction::toggled if it's a checkbox); and
              (b) will requite a lambda for the slot if OP wants to pass an index to identify item pressed.

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

              Thanks all. I need to digest all of this.
              I Think my main misunderstanding is confusing
              monitoring signal (menu sender r ) triggered by bool (action) and same signal triggered by
              value - index in my case .

              As of now it is a matter to put it all together...

              A 1 Reply Last reply
              0
              • A Anonymous_Banned275

                Thanks all. I need to digest all of this.
                I Think my main misunderstanding is confusing
                monitoring signal (menu sender r ) triggered by bool (action) and same signal triggered by
                value - index in my case .

                As of now it is a matter to put it all together...

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

                @AnneRanch
                CORRECTION

                I am sorry I had wrong debug setting

                This works
                connect(subMenu[index] , &QMenu::triggered, this,&MainWindow_Bluetooth::testSlot);

                Can we all agree
                subMenu[index] is sender and works
                this is obviously OK
                &MainWindow_Bluetooth::testSlot) is "receiver" OK

                the problem is with SIGNAL
                it should trigger from QAction "triggered" or " checked ".....

                I was wrong -

                sender is QMenu and sends "triggered"

                I need to work on "checked" and passing current index.

                When I get the SIGNAL working I can work on detecting the sender index...

                Axel SpoerlA 1 Reply Last reply
                0
                • A Anonymous_Banned275

                  @AnneRanch
                  CORRECTION

                  I am sorry I had wrong debug setting

                  This works
                  connect(subMenu[index] , &QMenu::triggered, this,&MainWindow_Bluetooth::testSlot);

                  Can we all agree
                  subMenu[index] is sender and works
                  this is obviously OK
                  &MainWindow_Bluetooth::testSlot) is "receiver" OK

                  the problem is with SIGNAL
                  it should trigger from QAction "triggered" or " checked ".....

                  I was wrong -

                  sender is QMenu and sends "triggered"

                  I need to work on "checked" and passing current index.

                  When I get the SIGNAL working I can work on detecting the sender index...

                  Axel SpoerlA Offline
                  Axel SpoerlA Offline
                  Axel Spoerl
                  Moderators
                  wrote on last edited by
                  #11

                  @AnneRanch said in Passing a value to slot...:

                  Can we all agree
                  subMenu[index] is sender

                  Yes.

                  and works

                  No.

                  The menu is just a visual tree that presents actions in a meaningful order. Actions like e.g. "Save file" can be presented in a menu (File->Save), and the same action can be triggered by a Save button on a menu bar. In that case, you don't want to connect to the menu and the button. You want to connect just to the action, because the application doesn't have to know how the action was triggered.

                  That said, the QMenu::triggered signal is more or less a helper. It tells you, that an action in the menu has been triggered. Its QAction *actionargument tells you which one it was.
                  What probably goes wrong in your case: The action you want to react to, isn't located in the menu you connect it to. It's located in another submenu. That's why nothing happens.

                  In my opinion, there is no need to keep your QMenuobjects in an array. They are (hopefully) parented to some other widget, so they won't leak. It's better to keep track of your QActionpointers and connect them to the right slots.

                  I am not going to refer you to the documentation again. @Christian-Ehrlicher has. Have you ever looked at it? An answer would be nice, but as usual we won't get it.

                  Software Engineer
                  The Qt Company, Oslo

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Anonymous_Banned275
                    wrote on last edited by
                    #12

                    OK, I am getting of the subject,,,

                    I agree that QMenu SIGNAL is pretty limited - but I need to have WORKING code and I can tune / modify it later.

                    I was looking at mapping example and that was too convoluted...

                    I am now trying lambda and need code help with "
                    processAction() parameter .
                    The example uses "text" and I like to have the actual "index". :

                    connect(subMenu[index] , &QMenu::triggered, this ,
                             [=]() { this->processAction()  );
                    
                    
                    Here is the function definition snippet 
                    
                     void   MainWindow_Bluetooth::processAction(QAction ???   )
                     {
                    #ifdef LAMBDA
                                            text = "TASK DEBUG connect... "; //connect
                                            text += Q_FUNC_INFO;
                    
                    
                    JonBJ 1 Reply Last reply
                    0
                    • A Anonymous_Banned275

                      OK, I am getting of the subject,,,

                      I agree that QMenu SIGNAL is pretty limited - but I need to have WORKING code and I can tune / modify it later.

                      I was looking at mapping example and that was too convoluted...

                      I am now trying lambda and need code help with "
                      processAction() parameter .
                      The example uses "text" and I like to have the actual "index". :

                      connect(subMenu[index] , &QMenu::triggered, this ,
                               [=]() { this->processAction()  );
                      
                      
                      Here is the function definition snippet 
                      
                       void   MainWindow_Bluetooth::processAction(QAction ???   )
                       {
                      #ifdef LAMBDA
                                              text = "TASK DEBUG connect... "; //connect
                                              text += Q_FUNC_INFO;
                      
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #13

                      @AnneRanch

                      and I like to have the actual "index". :

                      connect(subMenu[index] , &QMenu::triggered, this ,
                               [=]() { this->processAction(index); } );
                      
                      void   MainWindow_Bluetooth::processAction(int index)
                      {
                      }
                      
                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Anonymous_Banned275
                        wrote on last edited by
                        #14

                        THANKS !
                        The code works...
                        But the index is wrong - it is final index of the main loop .... something to work on
                        I actually need both indexes - main and sub loop..
                        However - this lambda "connect" is the main and final code and that is a HUGE help - thanks

                        JonBJ 1 Reply Last reply
                        1
                        • A Anonymous_Banned275

                          THANKS !
                          The code works...
                          But the index is wrong - it is final index of the main loop .... something to work on
                          I actually need both indexes - main and sub loop..
                          However - this lambda "connect" is the main and final code and that is a HUGE help - thanks

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

                          @AnneRanch
                          The index passed here should be the one used in the connect()'s subMenu[index], each one of those having its own connect() passing its own index value. subMenu[0] should be passing 0, subMenu[1] should be passing 1 , etc.

                          I don't know what the other thing is, but if you have another one, say i n variable, other, you can pass multiple parameters like:

                          int other = 999;
                          connect(subMenu[index] , &QMenu::triggered, this ,
                                   [=]() { this->processAction(other, index); } );
                          
                          void   MainWindow_Bluetooth::processAction(int other, int index)
                          {
                          }
                          

                          Yes, lambdas are the way to pass arbitrary values --- things in addition to anything the signal might pass --- to slots from your code. Syntax can get tricky, but they are very flexible/powerful.

                          A 1 Reply Last reply
                          2
                          • A Offline
                            A Offline
                            Anonymous_Banned275
                            wrote on last edited by
                            #16

                            @JonB said in Passing a value to slot...:

                            int other = 999;
                            connect(subMenu[index] , &QMenu::triggered, this ,
                            = { this->processAction(other, index); );

                            Thanks again, you are very helpful with this post .

                            Minor detail;
                            you example is missing the closing
                            "}" - but the compiler catches that, no big deal.

                            JonBJ 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @AnneRanch
                              The index passed here should be the one used in the connect()'s subMenu[index], each one of those having its own connect() passing its own index value. subMenu[0] should be passing 0, subMenu[1] should be passing 1 , etc.

                              I don't know what the other thing is, but if you have another one, say i n variable, other, you can pass multiple parameters like:

                              int other = 999;
                              connect(subMenu[index] , &QMenu::triggered, this ,
                                       [=]() { this->processAction(other, index); } );
                              
                              void   MainWindow_Bluetooth::processAction(int other, int index)
                              {
                              }
                              

                              Yes, lambdas are the way to pass arbitrary values --- things in addition to anything the signal might pass --- to slots from your code. Syntax can get tricky, but they are very flexible/powerful.

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

                              @JonB Yes , I expect the sender to pass "its index", but I need to check why it is passing main loop final index. That is just a minor matter of cleaning up the code- now when it is running as expected.
                              Thanks very much for fixing the lambda "connect".

                              1 Reply Last reply
                              0
                              • A Anonymous_Banned275

                                @JonB said in Passing a value to slot...:

                                int other = 999;
                                connect(subMenu[index] , &QMenu::triggered, this ,
                                = { this->processAction(other, index); );

                                Thanks again, you are very helpful with this post .

                                Minor detail;
                                you example is missing the closing
                                "}" - but the compiler catches that, no big deal.

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

                                @AnneRanch said in Passing a value to slot...:

                                Minor detail;
                                you example is missing the closing
                                "}" - but the compiler catches that, no big deal.

                                You are correct, my bad. I just type these examples in, my eyesight is not what it used to be! (I believe) I have corrected both my previous posts to have that missing }.

                                1 Reply Last reply
                                0
                                • Axel SpoerlA Axel Spoerl referenced this topic on

                                • Login

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