我需要将按钮连接到另一个类的成员函数。这里是类的代码:
int g_switch_value = 0;
int filterInt = 0;
int lastfilterInt = -1;
void MoyenEtMedian::switch_callback(int position, void* object)
{ MoyenEtMedian* moyetmed = (MoyenEtMedian*) object;
filterInt = position;
}
void MoyenEtMedian::exec(void)
{
const char* name = "Filtres";
IplImage* img = cvLoadImage( "image.png" );
IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3 );
cvNamedWindow( name, 1 );
cvShowImage(name, out);
// Create trackbar
cvCreateTrackbar2( "Filtre", name, &g_switch_value, 1, &MoyenEtMedian::switch_callback, this );
while( 1 ) {
switch( filterInt ){
case 0:
cvSmooth( img, out, CV_BLUR, 7, 7 );
break;
case 1:
cvSmooth( img, out, CV_MEDIAN, 7, 7 );
break;
}
if(filterInt != lastfilterInt){
cvShowImage(name, out);
lastfilterInt = filterInt;
}
if( cvWaitKey( 15 ) == 27 )
break;
}
cvReleaseImage( &img );
cvReleaseImage( &out );
cvDestroyWindow( name );
}
这是 GUI 的 .cpp(使用 Qt Designer 创建):
FenPrincipale::FenPrincipale(QWidget *parent) :
QWidget(parent),
ui(new Ui::FenPrincipale)
{
ui->setupUi(this);
QObject::connect(ui->bMoyMed,SIGNAL(clicked()),MoyenEtMedian,SLOT(MoyenEtMedian::exec()));
}
我收到 MoyenEtMedian 的“未在此范围内声明”错误,即使直接传递它也是如此。
更新:#include 丢失。“未在此范围内声明”问题已解决。
但我还有一个:
“','标记之前的预期主要表达式”涉及:
QObject::connect(ui->bMoyMed,SIGNAL(clicked()),MoyenEtMedian,SLOT(exec()));
我已经在 moyenetmedian.h 文件中声明了 SLOT:
#ifndef MOYENETMEDIAN_H
#define MOYENETMEDIAN_H
#include "ui_fenprincipale.h"
class MoyenEtMedian
{Q_OBJECT
public:
MoyenEtMedian();
static void switch_callback(int position, void*);
public slots :
void exec(void);
};
#endif // MOYENETMEDIAN_H