求大神们帮忙啊,在debian6系统中运用Qt和Libpca混合编程,实现网络抓包工具,其中libpcap中有个循环抓包的函数pcap_loop();他要求回调函数必须是全局函数或静态函数,当我设置为静态函数时,就不能用信号传递数据到界面了,能帮我解释下吗?讨论下解决方法,先谢谢各位了!
------解决方案--------------------
你的静态函数多个参数 void *data;
void function(int a, char *b ... void *data)
{
YourClass *class = (YourClass *)data;
class->do_something;
}
掉用的时候,就把你的QT的类传进去:
YourClass *class = new YourClass();
function(1, .... (void *)class);
这个是一般回调函数的技巧
------解决方案--------------------
给你写了个例子:
Static_function.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-27T18:03:33
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = Static_function
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
myobject.cpp
HEADERS += \
myobject.h
main.cpp
#include <QCoreApplication>
#include "myobject.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
myObject *obj = new myObject;
showMsg *msg = new showMsg;
QObject::connect(obj, SIGNAL(mySignal(QString)),msg, SLOT(slotShowMsg(QString)));
imNonClassFunction((void *)obj);
return a.exec();
}
myobject.h
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QString>
#include <QDebug>
int imNonClassFunction(void *data);