我正在学习QT,并试图让我的信号和插槽工作.我没有运气.
这是我的主菜
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();
}
这是我对信号和插槽的悲伤尝试:
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);
}
这是FilmWriter.cpp
void FilmInput::okButtonClicked(){
Film *aFilm=new Film();
aFilm->setDirector(this->edtDirector->text());
emit obtainFilmData(aFilm);
}
请协助我让信号和插槽工作,我花了几个小时,但我没有接近它的工作.我添加了上述评论中收到的错误.
问候
最佳答案 okButton已经是一个指针,那么你应该删除&符号:
QObject::connect(fi.okButton, SIGNAL(clicked()), &fi, SLOT(okButtonClicked()));
(实际上,请确保在FilmInput的构造函数中创建按钮…)
接下来,您的方法签名与您在连接中所说的不匹配:给定您的功能,应该是
Object::connect(&fi, SIGNAL(obtainFilmData(Film*)), &fw, SLOT(saveFilmData(Film*)));
这将工作,因为你正在交换指针,然后Qt可以复制它.否则,您的类型应该注册.