Qt+libpcap混合编程
求大神们帮忙啊,在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
#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();
}
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QString>
#include <QDebug>
int imNonClassFunction(void *data);
class showMsg : public QObject
{
Q_OBJECT
public:
explicit showMsg(QObject *parent = 0){}
signals:
public slots:
void slotShowMsg(const QString &msg);
};
class myObject : public QObject
{
Q_OBJECT
public:
explicit myObject(QObject *parent = 0);
void sendSignalFuncion();
signals:
void mySignal(const QString &txt);
public slots:
};
#endif // MYOBJECT_H
#include "myobject.h"
int imNonClassFunction(void *data)
{
myObject *obj = (myObject *)data;
obj->sendSignalFuncion();
return 0;
}
myObject::myObject(QObject *parent) :
QObject(parent)
{
}
void myObject::sendSignalFuncion()
{
emit mySignal("I am Signal!");
}
void showMsg::slotShowMsg(const QString &msg)
{
qDebug() << msg;
}