Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cannot pass parameter in SLOT



  • Hello, I am trying to declare an action in Custom menu Request as:

    void protocolForm::customMenuRequested(QPoint pos)
    {
        QMenu *menu = new QMenu(this);
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(0))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(1))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(2))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(3))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(4))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(5))));
    }
    

    enumArray is Qstring vector. When I try that in this way with integer parameter in sendEnumSlot() slot, that gives me

    QObject::connect: (receiver name: 'widget')
    QObject::connect: No such slot protocolForm::sendEnumSlot(0)
    warnings

    However when I try the same thing with declaring the sendEnumSlot() without any parameters in it and add action as:

    void protocolForm::customMenuRequested(QPoint pos)
    {
        QMenu *menu = new QMenu(this);
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot()));
        menu->addAction(enumArray.value(1),this,SLOT((sendEnumSlot()));
        menu->addAction(enumArray.value(2),this,SLOT((sendEnumSlot()));
        menu->addAction(enumArray.value(3),this,SLOT((sendEnumSlot()));
       menu->addAction(enumArray.value(4),this,SLOT((sendEnumSlot()));
       menu->addAction(enumArray.value(5),this,SLOT((sendEnumSlot()));
    }
    
    

    That works. Am I missing something? How can I overcome this problem?


  • Moderators

    Signal-slot connections are between methods. You cannot specify concrete values for slot parameters - these are always taken from the signal.

    If you want to call the same slot with different value based on menu item, you can:

    • connect your action to lambda and call your slot with correct parameter inside
    • or call the same slot from all actions, but in the slot call sender() and infer which action triggered the call
    • or create separate slots for each of your actions

  • Moderators

    Signal-slot connections are between methods. You cannot specify concrete values for slot parameters - these are always taken from the signal.

    If you want to call the same slot with different value based on menu item, you can:

    • connect your action to lambda and call your slot with correct parameter inside
    • or call the same slot from all actions, but in the slot call sender() and infer which action triggered the call
    • or create separate slots for each of your actions


  • @sierdzio How can I use sender() function? That returns some address value I guess. How can I know which action refers to that address value?


  • Moderators

    auto action = qobject_cast<QAction*>(sender());
    if (action) {
      if (action->text() == enumArray.value(0)) {
        // First action triggered the slot
      }
    }
    


  • @sierdzio Thanks



  • @sierdzio There is a fourth option to provide a value to the slot: Qt has a helper class calls QSignalMapper. Haven't used it ever, though ;-)


Log in to reply