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...
Forum Updated to NodeBB v4.3 + New Features

Passing a value to slot...

Scheduled Pinned Locked Moved Unsolved C++ Gurus
18 Posts 8 Posters 3.3k 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.
  • 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