Search code examples
qtsignals-slots

Qt Signals and slots - No matching function for call


I am learning QT and am trying to get my signals and slots working. I am having no luck. Here is my Main

    int main(int argc, char** argv) {
      QApplication app(argc, argv);

      FilmInput fi;
      FilmWriter fw;
      QObject::connect (&fi->okButton, SIGNAL( clicked() ), &fi, SLOT( okButtonClicked() ) 

    ); //Error received Base operand of '->' has non-pointer type 'FilmInput'
     QObject::connect(&fi,SIGNAL(obtainFilmData(QVariant*)),&fw,SLOT(saveFilmData(QVariant*)));
//Error received No matching function for call to 'QObject::connect(Filminput*, const     char*, FilmWriter*, const char*)
      fi.show();
      return app.exec();

    }

and here is my sad attempt at signals and slots:

FilmInput.h

public:
        FilmInput();
        void okButtonClicked();
    QPushButton* okButton;
signals:
        void obtainFilmData(Film *film);
Here is FilmWriter.h

public slots:
    int saveFilm(Film &f);

Here is Film Input.cpp


void FilmInput::okButtonClicked(){
    Film *aFilm=new Film();
    aFilm->setDirector(this->edtDirector->text());
    emit obtainFilmData(aFilm);

}

Here is FilmWriter.cpp

void FilmInput::okButtonClicked(){
    Film *aFilm=new Film();
    aFilm->setDirector(this->edtDirector->text());
    emit obtainFilmData(aFilm);

}

Please assist me in getting the signals and slots to work, I have spent hours but am no closer to getting it working. I have added the errors received in my comments above.

Regards


Solution

  • okButton is already a pointer, then you should remove the ampersand:

    QObject::connect(fi.okButton, SIGNAL(clicked()), &fi, SLOT(okButtonClicked()));
    

    (actually, be sure you create the button in FilmInput' constructor...)

    Next, your methods signature doesn't match what you say in connect: given your functions, should be

    Object::connect(&fi, SIGNAL(obtainFilmData(Film*)), &fw, SLOT(saveFilmData(Film*)));
    

    this will work, because you are exchanging a pointer, and then Qt can make a copy of it. Otherwise, your type should be registered.