Search code examples
c++qtsignals-slots

Passing custom class to connect (signals and slot)


i'm trying to create a new dialog with some text input fields. These text areas, on a button click (as a confirmation), have to be inserted into a custom class vector (through existing methods).

My doubt is, how can i fit the connect function to pass the custom class as a reference? I'm very newbie on this, please forgive my dumbness. This is (part of) my code.

#include "aggiungi.h"
#include "ui_aggiungi.h"


aggiungi::aggiungi(clienti& c, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::aggiungi)
{

ui->setupUi(this);

connect(ui->inserisci,SIGNAL(clicked()),this,SLOT(pulsanteInserisci(c)));

}

aggiungi::~aggiungi()
{
  delete ui;
}

void aggiungi::pulsanteInserisci(clienti& c) {

string nome = ui->textNome->toPlainText().toStdString();
unsigned int cc = ui->textCod->toPlainText().toUInt();
string piva = ui->textPIva->toPlainText().toStdString();

cliente* nc = new cliente(nome,cc,piva);
c.aggiungi(*nc);

}

All i want to is to be able to pass the "clienti" object in order to execute the commands when the button is clicked. This solution is (obviously) not working but how can i correct it? What's your suggestion?

I'll also paste the error i'm getting out of the application output:

QObject::connect: No such slot aggiungi::pulsanteInserisci(c) 
in..\Progetto2016\git\database\aggiungi.cpp:9
QObject::connect:  (sender name:   'inserisci')
QObject::connect:  (receiver name: 'aggiungi')

Thanks in advance


Solution

  • You cannot pass values to SIGNAL() or SLOT() macros. You must pass the signature. Furthermore, the signal and slot must be compatible. In other words, the signatures must match. See the documentation for more details.

    Having said that, there is no way you can connect a button's clicked() signal to a slot that requires any argument. A simple solution is to store clienti as a member variable and use the member variable in pulsanteInserisci().