Search code examples
c++qtqt-designer

Connect -> No such slot on Window


I am creating a Qt Designer force calculation program, the principle is to give input values on a QDoubleSpinBox and do the calculation through a method, When i try to connect a Pushbutton to activate the calculation i get a "No such slot error"

I made sure to declare public slots in the .h file it still does not give anything, the function 'Lunghezza' is the one that do the calculation

Header File : Window.h

class Window;
}

class Window : public QMainWindow
{
    Q_OBJECT

public:
    explicit Window(QWidget *parent = nullptr);
    ~Window();

public slots:


  void Lunghezze(double Longeur1 , double Longeur2 , double Largeur , double Hauteur,double *T1 ,double *T2 , double *T3 ,double *T4);


private:
    Ui::Window *ui;
};

My cpp file Window.cpp

    QMainWindow(parent),
    ui(new Ui::Window)

{

    ui->setupUi(this);


double L1=ui->L1ValF->value();
double L2=ui->L2ValF->value();       // Getting the value from QDoubleSpinBox
double l=ui->lValF->value();
double H=ui->HValF->value();
double F=ui->FValF->value();

   connect(ui->Calculate, SIGNAL(clicked()),SLOT(Lunghezze(L1,L2,l,H,F,0,0,0,0)));


}


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


void Window::Lunghezze(double Longeur1 , double Longeur2 , double Largeur , double Hauteur,double *T1 ,double *T2 , double *T3 ,double *T4){

float C1=hypot(Longeur1,Largeur/2);
float C2=hypot(Longeur1,Largeur/2);
float C3=hypot(Longeur2,Largeur/2);
float C4=hypot(Longeur2,Largeur/2);
                                                // CALCOLO LUNGHEZZA FILI
*T1=hypot(C1,Hauteur);
*T2=hypot(C2,Hauteur);
*T3=hypot(C3,Hauteur);
*T4=hypot(C4,Hauteur);

}

I have also delated the moc & .o files but still get this message!

QObject::connect: No such slot Window::Lunghezze(L1,L2,l,H,F,0,0,0,0) in ../calculo16/window.cpp:23 QObject::connect: (sender name:
'Calculate') QObject::connect: (receiver name: 'Window')


Solution

  • Use a lambda and the modern form of QObject slot connection:

    connect(ui->Calculate, &QPushButton::clicked, this, [=]() {
      Lunghezze(L1,L2,l,H,F,0,0,0,0);
    });
    

    Note that this will capture the values of L1,L2,... at construction time. If you want to fetch them when the button is clicked, move the ...->value calls inside the lambda.

    Alternatively, add a new slot (sans parameters) to Window:

    void Window::on_Calculate_clicked() {
     double L1=ui->L1ValF->value();
      double L2=ui->L2ValF->value();
      double l=ui->lValF->value();
      double H=ui->HValF->value();
      double F=ui->FValF->value();
      Lunghezze(L1,L2,l,H,F,0,0,0,0);
    }
    

    This name should enable signal-slot auto-connection, but you can always connect it manually.