码迷,mamicode.com
首页 > 其他好文 > 详细

回调函数的使用

时间:2015-05-14 11:30:02      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

在oc中,我都是使用块函数的方法来传递参数和事件调用,在C++也可以使用回调函数来实现

 



TestClass.h
#pragma once

struct dlgStruct
{
    void* pObj;
    int pos;
};

typedef void WaitFunc(int a);
typedef void dlgFunc(dlgStruct *dlg);


class TestClass
{
public:
    TestClass(void);
    ~TestClass(void);

    WaitFunc *m_pWaitFunc;
    dlgFunc *m_dlgFunc;
    dlgStruct *m_dlgStruct;

    void setFunc(WaitFunc *pFunc);
    void setDlgFunc(dlgFunc *dlgFunc,dlgStruct *dlg);
  void setFuncMessage();
    void sendDlgFuncMessage();
    int value;
};

TestClass.cpp
#include "TestClass.h"
#include <string.h>
TestClass::TestClass(void)
{
    value = 0;
    m_dlgStruct = new dlgStruct;
}


TestClass::~TestClass(void)
{
}

void TestClass::setFunc(WaitFunc *pFunc)
{
    m_pWaitFunc = pFunc;
}

void TestClass::setFuncMessage()
{
    m_pWaitFunc(value);
    value++;
}

void TestClass::setDlgFunc(dlgFunc *dlgFunc,dlgStruct *dlg)
{
    m_dlgFunc = dlgFunc;
    memcpy(m_dlgStruct,dlg,sizeof(m_dlgStruct));
}

void TestClass::sendDlgFuncMessage()
{
    m_dlgStruct->pos = value;
    value++;
    m_dlgFunc(m_dlgStruct);
}

FunctionPointer.h

#ifndef FUNCTIONPOINTER_H
#define FUNCTIONPOINTER_H

#include <QtGui/QDialog>
#include "ui_functionpointer.h"
#include "TestClass.h"

class FunctionPointer : public QDialog
{
    Q_OBJECT

public:
    FunctionPointer(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~FunctionPointer();
private slots:
      void startScanSlots();
      void startScanSlots_2();
public:
    Ui::FunctionPointerClass ui;
    TestClass *pTestClass;
};

#endif // FUNCTIONPOINTER_H

FunctionPointer.cpp
#include "functionpointer.h"

FunctionPointer *point;
void SendMessage(int a)
{
    int b = a;
    QString str = QString("FuncPoint:%1").arg(b);
    point->ui.pushButton->setText(str);
}

void SendDlgMessage(dlgStruct *dlg)
{
    FunctionPointer *pointDlg = (FunctionPointer *)dlg->pObj;
    int b = dlg->pos;
    QString str = QString("FuncStruct:%1").arg(b);
    pointDlg->ui.pushButton_2->setText(str);
    QString strText = pointDlg->ui.pushButton->text();
    pointDlg->ui.label->setText(strText);
}

FunctionPointer::FunctionPointer(QWidget *parent, Qt::WFlags flags)
    : QDialog(parent, flags)
{
    ui.setupUi(this);
    pTestClass = new TestClass;
   connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(startScanSlots()));
   connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(startScanSlots_2()));
    pTestClass->setFunc(SendMessage);

    dlgStruct dlg;
    dlg.pObj = this;
    dlg.pos = 0;
    pTestClass->setDlgFunc(SendDlgMessage,&dlg);

    point = this;
}

FunctionPointer::~FunctionPointer()
{
}

void FunctionPointer::startScanSlots()
{
    pTestClass->setFuncMessage();
}

void FunctionPointer::startScanSlots_2()
{
   pTestClass->sendDlgFuncMessage();
}

main.cpp 
#include
"functionpointer.h"
#include
<QtGui/QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  FunctionPointer w; w.show();
  return a.exec();
}

 

回调函数的使用

标签:

原文地址:http://www.cnblogs.com/Travis990/p/4502688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!