C++回撥函式跨模組
阿新 • • 發佈:2019-02-04
#ifndef CONTROLLER_H #define CONTROLLER_H // // 檔名:Controller.h // 工程名:CppCallback // 開發環境:MacOS 10.13.1 Qt5.9.1 // 簡介:回撥函式使用 // 建立日期:Created by chenyijun on 2018/03/18. // 版權:Copyright © 2018年 chenyijun. All rights reserved. // #include <QObject> typedef void (*CallBackFuntion)(void *userParam, int param1, int param2, bool param3); class Controller : public QObject { Q_OBJECT public: explicit Controller(QObject *parent = nullptr); void copyFileOperation(CallBackFuntion callback, void *userParam); signals: public slots: }; #endif // CONTROLLER_H
// // 檔名:Controller.cpp // 工程名:CppCallback // 開發環境:MacOS 10.13.1 Qt5.9.1 // 簡介:回撥函式使用 // 建立日期:Created by chenyijun on 2018/03/18. // 版權:Copyright © 2018年 chenyijun. All rights reserved. // #include "Controller.h" #include <QTime> #include <QFile> #include <QDebug> Controller::Controller(QObject *parent) : QObject(parent) { } void Controller::copyFileOperation(CallBackFuntion callback, void *userParam) { callback(userParam, 1, 0, true); QTime runTime; runTime.start(); QFile file("/Users/chenyijun/project/CppCallback/MonoFramework.pkg");//用一個大檔案測試才能看到效果 bool isCopy = file.copy("/Users/chenyijun/project/CppCallback/copyTest.pkg"); int useTime = runTime.elapsed();//耗時:毫秒 callback(userParam, 0, useTime, isCopy); }
#ifndef MAINWINDOW_H #define MAINWINDOW_H // // 檔名:mainwindow.h // 工程名:CppCallback // 開發環境:MacOS 10.13.1 Qt5.9.1 // 簡介:回撥函式使用 // 建立日期:Created by chenyijun on 2018/03/18. // 版權:Copyright © 2018年 chenyijun. All rights reserved. // #include <QMainWindow> #include <QTime> #include "Controller.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void initView(); void initData(); void initConnect(); /* 功能:複製檔案回撥測式 * 第一個引數:當前使用者指標, * 第二個引數:狀態,1表示開始,0表示結束 * 第三個引數:複製檔案耗時 * 第四個引數:複製是否成功 */ static void copyFileCallBack(void *userParam, int nStatus, int nTimeConsuming, bool isCopy); void callBackDeal(int nStatus, int nTimeConsuming, bool isCopy); public slots: void slotCopyFile(); private: Ui::MainWindow *ui; Controller *m_pController; int m_nType; }; #endif // MAINWINDOW_H
//
// 檔名:mainwindow.cpp
// 工程名:CppCallback
// 開發環境:MacOS 10.13.1 Qt5.9.1
// 簡介:回撥函式使用
// 建立日期:Created by chenyijun on 2018/03/18.
// 版權:Copyright © 2018年 chenyijun. All rights reserved.
//
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initView();
initData();
initConnect();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initView()
{
}
void MainWindow::initData()
{
m_pController = NULL;
m_nType = 10;
m_pController = new Controller(this);
}
void MainWindow::initConnect()
{
connect(ui->pushButton_CopyFile, SIGNAL(clicked()), this, SLOT(slotCopyFile()));
}
void MainWindow::slotCopyFile()
{
if(NULL != m_pController)
{
m_pController->copyFileOperation(copyFileCallBack, this);
}
}
void MainWindow::copyFileCallBack(void *userParam, int nStatus, int nTimeConsuming, bool isCopy)
{
MainWindow *pThis = (MainWindow*)userParam;
qDebug() << "copyFileCallBack======pThis->m_nType=====" << pThis->m_nType;
pThis->callBackDeal(nStatus, nTimeConsuming, isCopy);
}
void MainWindow::callBackDeal(int nStatus, int nTimeConsuming, bool isCopy)
{
qDebug() << "callBackDeal======nStatus=====" << nStatus << "======nTimeConsuming=" << nTimeConsuming << "======isCopy======" << isCopy;
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}