How to pass parameters to a SLOT function?



  • Hi guys,
    .
    I need to do some processing inside a SLOT function but this function must have an argument, such as:

    @
    public slot:

    void doSomething(double*);
    @

    .
    @
    connect(buttonOne, SIGNAL(clicked()), this, SLOT(doSomething(array)));
    @

    .
    Everything seems to work fine until I try to call the function by pressing the push button. I get the following message:

    Object::connect: No such slot MainWindow::doSometging(array)

    Can anyone tell me what is going on?

    Thank you in advance,


  • Moderators

    @
    connect(buttonOne, SIGNAL(clicked()), this, SLOT(doSomething(double *)));
    @

    This should work. But clicked() will not pass any data to your slot, so the pointer will be dangling. Plus you probably put the connect statement in a wrong place: it should be shown earlier, not on button click (but that depends on your design. Typically, connect statements are put in base widget constructors).



  • Hello! Thank you for your reply!

    Actually, I am coding a button that creates another buttons and the same function is set to each of these buttons.

    I didn't understand what you mean, sorry! Can you please explain in more details?

    Thank you again,


  • Moderators

    Always check "the docs":http://qt-project.org/doc/qt-4.8/signalsandslots.html first.

    Ok, so what I meant was this:
    When you have a signal-slot connection, and the signal is emitted, it will "carry" the argument it's invoked with (well, so to speak). For example, QAbstractButton has a signal named toggled(bool) - it will always be sent together with this boolean information. If your corresponding slot has a use for it (example: mySlot(bool isToggled)) this value will be passed to it.

    In your example, however, the clicked() signal does not come with any parameter - so it will not pass any data to doSomething(double *pointer) - and in truth, MOC will not allow such a connection (again - see the documentation, it's all explained there).



  • Actually, I just did like this:
    @
    signals:
    void clicked(QPushButton*);
    public slots:
    void doSomething(QPushButton*);
    @
    .
    @
    connect(buttonOne, SIGNAL(clicked(QPushButton*)), this, SLOT(doSomething(buttonTwo)));
    @
    .
    But still getting the same error message.


  • Moderators

    Does the buttonOne contain your custom signal? Have you rerun moc (do a full rebuild of your project if in doubt. That is: clean, run qmake, build)?



  • Oops! Sorry!

    buttonOne and buttonTwo are the same! Sorry! Please, read:

    @
    connect(buttonOne, SIGNAL(clicked(QPushButton*)), this, SLOT(doSomething(buttonOne)));
    @

    The idea is, when the button is clicked, it will change its icon.


  • Moderators

    You are doing it wrong. Please, read the documentation first, I've already linked you to it.



  • Sorry, but I guess that I am doing what is written in the documentation. If I am doing it wrong I guess it means that I didn't really understand what is in the documentation... So if someone here doens't mind, please, provide another exemple.


  • Moderators

    You cannot control what will be sent through the signal in the connect statement - only the sender itself uses "emit" keyword (well, can use it) and provides concrete argument (QPushButton pointer in your case).

    Also, connect cannot control the receiving end either, so you can't put a concrete object there (buttonOne in SLOT is wrong). Connect statement acts only as a connecting cord, it has no influence on the information apart from providing QObject->sender() and - optionally - queuing the connection.

    And since I don't know where you declare your signal, where does "this" point to in your code, I am mostly guessing your setup anyway.



  • Hello! Thank you for your reply.

    I’ve found this post, that I guess “answer” part of my problem:

    http://www.qtcentre.org/archive/index.php/t-33960.html

    But now, what I need is connect an array of buttons, each one of them is created by the user (meaning that they are stored in a list – or QList).

    I created an update function as follow:
    @
    void MainWindow::update(){
    signalMapper = new QSignalMapper(this);

        for (int ii = 0; ii < btnList.size(); ii++){
            connect(btnList[ii].returnButton(), SIGNAL(clicked()), signalMapper, SLOT(map()));
            signalMapper->setMapping(btnList[ii].returnButton(), id);
            id++;
        }
     
    }
    

    @
    Where btnList is a linked list containing all the buttons created by the GUI user, and returnButton() returns QPushButton*.

    But it is still not working… Do you have any idea?



  • Also, I created a class AddButton, in order to encapsulate all the info that I need for my code.

    @
    class AddButton
    {
    public:
    AddButton();
    AddButton(QString, QIcon);
    QPushButton* returnButton(void);
    QIcon returnIcon(void) {return *icon;}
    void setGeometry();
    void setID(int id1) {this->id = id1;}
    QString returnName() {return this->btnName;}
    int returnID() {return this->id;}
    ~AddButton() {delete newButton; delete icon;}

    protected:
    QPushButton* newButton;
    QIcon *icon;
    QString btnName;
    int id;
    };
    @

    And the "New Button" code is:

    @
    void MainWindow::on_btnNew_clicked()
    {
    QIcon *icon = new QIcon("/home/gabe/GGW.jpg");
    AddButton *newButton = new AddButton(QString("Teste"), *icon);

    newButton->setGeometry();
    ui->layoutGrid->addWidget(newButton->returnButton(), returnRow(), returnCol());
    increment();
    
    btnList.push_back(*newButton);
    update();
    QString qName = newButton->returnName();
    connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(displayImg(qName)));
    

    }
    @



  • @sierdzio

    hi I am having same problem.

    for(int i=0;i<2;i++)
    {
    for(int j=0;j<2,j++)
    {
                snapshot_action[k]=new QAction(this);
                snapshot_action[k]->setIconText("Snapshot");
                control_menu[k]->addAction(snapshot_action[k]);
                connect(snapshot_action[k], &QAction::triggered, this, &SimplePlayer::snap_fun);
    }
    }
    

    With initially k=0;
    I have omitted code which lays out four views each with snapshot action. ( for simplicity)

    Now the problem is when I press snapshot action button of any view I should know somehow that snapshot action button of THAT particular window is pressed.

    How can I ?
    How would I pass value of to my snap function?
    Do I need to store values of 'k' in some array? (some bad idea)


  • Moderators

    @mit_cruze said in How to pass parameters to a SLOT function?:

    How would I pass value of to my snap function?

    Use QObject::sender() in the slot.



  • @sierdzio thanks


Log in to reply
 

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