Qt Signals and slots - No matching function for call

∥☆過路亽.° 提交于 2019-12-10 15:51:07

问题


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


回答1:


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.




回答2:


The first error is caused by &fi->okButton because -> has a higher precedence than &. See Operator Precedence. Easily fixed with (&fi)->okButton.

The second error is likely caused by one or both objects not inheriting from QObject. Signals and slots can only work between QObjects. Just a quick checklist:

  1. Both sender and receiver must inherit from the QObject class
  2. The Q_OBJECT macro must be used in the definition of each class
  3. You must generate the appropriate meta-object code and compile it into your project. See Meta-Object Compiler

Also, if you're using Qt version 5.0 or higher, you should start passing function pointers to the connect methods instead of using the SIGNAL and SLOT macros. It'll provide compile-time checking of the connections.



来源:https://stackoverflow.com/questions/18284936/qt-signals-and-slots-no-matching-function-for-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!