1

I need to connect a button to a member function from another class. Here the class' code :

 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 );

}

Here's the .cpp of the GUI (created with 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()));

}

I'm getting the "not declard in this scope" error for MoyenEtMedian, even when passing it directly.

UPDATE : #include was missing. The "not declared in this scope" issue is resolved.

But I have another one :

"expected primary-expression before ',' token" concerning :

QObject::connect(ui->bMoyMed,SIGNAL(clicked()),MoyenEtMedian,SLOT(exec()));

I have declared the SLOT in the moyenetmedian.h file :

 #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
  • The third parameter to connect should be a pointer to the receiver class. Also i don't think you need to use the class name inside the SLOT macro. – dowhilefor Dec 10 '12 at 16:50

2 Answers2

4

Until Qt 5.0, the destination of a connection has to be declared as a SLOT in the class declaration.

Qt 5.0 introduced some new syntax for connection. One of them allows you to connect a signal to any member function: http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#connect-4

Stephen Chu
  • 12,724
  • 1
  • 35
  • 46
1

You need to create an object MoyenEtMedian here a sample with FenPrincipale member (or you can pass your object as argument to the FenPrincipale contructor if it was already created).

FenPrincipale::FenPrincipale(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FenPrincipale)
{
    ui->setupUi(this);
    moyenEtMedian = new MoyenEtMedian();
    QObject::connect(ui->bMoyMed,SIGNAL(clicked()), moyenEtMedian, SLOT(exec()));
}

In your code MoyenEtMedian is a class not an object.

Kirween
  • 1,472
  • 1
  • 19
  • 24
  • I did this, and I got new errors : " conversion from 'MoyenEtMedian*' to non-scalar type 'MoyenEtMedian' requested" and "no matching function for call to 'FenPrincipale::connect(QPushButton*&, const char [11], MoyenEtMedian&, const char [8])'" – El-Hadj Chikhaoui Dec 10 '12 at 18:54
  • `moyenEtMedian = new MoyenEtMedian();` is an error, should be `moyenEtMedian = new MoyenEtMedian;` http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets – Martin Beckett Dec 10 '12 at 19:21
  • You have to declare in the `FenPrincipale` class declaration your member like this : `MoyenEtMedian *moyenEtMedian;` – Kirween Dec 10 '12 at 20:06
  • 1
    Add this `class MoyenEtMedian;` before `FenPrincipale` class declaration (.h file). – Kirween Dec 10 '12 at 20:13