Parameters in Slots



  • Hey there,

    I am having some trouble with slots & signals in my program.
    my mainView class contains a slot called 'launchApp', launchApp accepts a QString as a parameter as you can see below:
    @void mainView::launchApp(QString appName)
    {
    if(appName == "music")
    {

    }
    

    }@

    meanwhile I have a button in another class called 'sideBar'. when this button is clicked it calls mainView::launchApp:
    @
    connect(launchMusicAppButton, SIGNAL(clicked()), mainView_object_pointer, SLOT(launchApp("music")));
    @
    when I run my application I get this error:
    QObject::connect: No such slot mainView::launchApp("music")

    I believe that the problem lies in how I am declaring the parameter of launchApp. How can I have "music" as a parameter of launchApp? It is designed to accept QStrings as you can see above.

    Thanks for your help!



  • I would read up on the documentation about signals and slots. It seems that you missed something. The connect should be failing on you. You pass parameters through calling emit which then calls the slot with the parameter. You either have to create a slot to connect to clicked and then a signal to fire in the slot that passes a string, or like into QSignalMapper.



  • so the parameter should be in clicked() instead of launchApp()?



  • No, you have to catch the clicked signal, and in that slot refire another signal with a string, or use a QSignalMapper to map the clicked signal to a slot that takes a string. The parameters of the signal slot pair have to be the same. The clicked signal, if i recall doesn't have any parameters.



  • Something of this nature.

    @
    siganls:
    void launchApp(QString name);

    protected slots:
    void myClicked()
    @

    @
    connect(button,SIGNAL(clicked()), object,SLOT(myClicked()));
    connect(object,SIGNAL(launchApp(QString)),someOtherObj,SLOT(launchApp(QString)));
    @

    @
    void myObject::myClicked()
    {
    emit launchApp("music");
    }
    @



  • Also consider using QSignalMapper. Alternatively, you could use a lambda function as your slot.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.