zoukankan      html  css  js  c++  java
  • 回调函数的使用

    在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();
    }
  • 相关阅读:
    NPOI使用手册
    MySQL索引-B+树
    IDEA Pycharm WebStorm JetBranis全版本 2020年 最新激活方式
    SpringBoot整合MyBatis
    js-cookie的用法
    Vue项目devServer.proxy代理配置详解
    子级div设置margin属性影响父级位置
    深拷贝和浅拷贝的区别和与原理
    css如何将div画成三角形
    macos10.15.4以上svn报错svn: error: The subversion command line tools are no longer provided by Xcode解决
  • 原文地址:https://www.cnblogs.com/Travis990/p/4502688.html
Copyright © 2011-2022 走看看