成员功能作为Qt插槽 [英] Member function as a Qt Slot

查看:101
本文介绍了成员功能作为Qt插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将按钮连接到另一个类的成员函数.这是课程的代码:

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

}

这是GUI的.cpp(使用Qt Designer创建):

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

}

即使直接传递MoyenEtMedian,我也会收到未在此范围内声明"错误.

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

更新:缺少#include.解决了未在此范围内声明"问题.

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

但是我还有一个:

与','令牌之前的预期主要表达式"有关:

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

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

我已经在moyenetmedian.h文件中声明了SLOT:

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

推荐答案

直到Qt 5.0,连接的目的地必须在类声明中声明为SLOT.

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

Qt 5.0引入了一些用于连接的新语法.其中之一允许您将信号连接到任何成员函数: http://qt-project.org/doc/qt -5.0/qtcore/qobject.html#connect-4

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

这篇关于成员功能作为Qt插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆