PDA

View Full Version : how to define the callback function in QThread?



cxl2253
27th March 2007, 08:13
I just want to use the callback in QThread,but cannot define the static varibale.The following is the code :
/////////////////// qt_metaobject not found the static varible code ////////
typedef void (__stdcall * STORESCPCALLBACK)(char* progress, char * filename);
class mythread :public QThread
{
Q_Object
private:
static char * mystr1;
static char * mystr2;
static void storeSCPCallback(char* progress, char * filename);
protected:
void myfuncion(STORESCPCALLBACK test);
void run();
}

in the function myfuncion,i callback the storeSCPCallback; in storeSCPCallback,i used the two static variable.
when compiled it ,vc7 always report the error, qt_metaobject not define the varible mystr1and mystr2.
when i move the static varible out of the QThread,it's ok. why???
///////////// ok code////////////
static char * mystr1;
static char * mystr2;
class mythread :public QThread
{
Q_Object
private:
static void storeSCPCallback(char* progress, char * filename);
protected:
void myfuncion(STORESCPCALLBACK test);
void run();
}

danadam
27th March 2007, 10:18
when compiled it ,vc7 always report the error, qt_metaobject not define the varible mystr1and mystr2.
when i move the static varible out of the QThread,it's ok. why???

Do you define your static variables somewhere outside class definition? Static variables in class definition are only declarations:

// MyClass.h
class MyClass {
static int myStatic; // this is only declaration
}
// MyClass.cpp
#include "MyClass.h"

int MyClass::myStatic = 0; // here is definition

high_flyer
27th March 2007, 10:25
What version of Qt are you using?
Why don't you just use signals/slots (which is nothing more than call backs) or events?
P.S
Please use code tags for code

cxl2253
28th March 2007, 01:51
thanks,danadam,you are right. but the another question comes: if the static variable is just fucntion define variable or enum define variable, the compiler still report error,"the variable have defined" .how to do it?
1. typedef void (__stdcall * STORESCUCALLBACK)(T_StoreProgress* progress);
2. class myclass
3 {
4 static STORESCUCALLBACK mystorecallback;
5 }
6 STORESCUCALLBACK myclass::mystorecallback=NULL;

danadam
28th March 2007, 13:16
typedef void (__stdcall * STORESCUCALLBACK)(T_StoreProgress* progress);
class myclass
{
static STORESCUCALLBACK mystorecallback;
}
STORESCUCALLBACK myclass::mystorecallback=NULL;
I can't see nothing wrong in this example. The error maybe caused by something else. Below is a sample code with static pointer to function and with passing pointer to function to method:
// Test.h
#ifndef TEST_H_
#define TEST_H_

typedef int (* FUNC_PTR)(int, int);

class Test
{
public:
static int add(int l, int r);
static int mul(int l, int r);
static FUNC_PTR funcPtr; // declaration of static pointer to function

void doTheJob();

private:
int callback(FUNC_PTR f, int l, int r); // declaration of function with parameter
};

#endif
// Test.cpp
#include "Test.h"
#include <iostream>

FUNC_PTR
Test::funcPtr = NULL; // definition of static pointer to function

int
Test::add(int l, int r) {
return l+r;
}

int
Test::mul(int l, int r) {
return l*r;
}

int
Test::callback(FUNC_PTR f, int l, int r) {
return f(l, r);
}

void
Test::doTheJob() {
int result = callback(funcPtr, 2, 5);
std::cout << "result = " << result << std::endl;
}
// main.cpp
#include "Test.h"
int main ()
{
Test test;
test.funcPtr = Test::add;
test.doTheJob();

test.funcPtr = Test::mul;
test.doTheJob();
}

cxl2253
30th March 2007, 03:11
but if declare static variable in class,it will report error.

danadam
30th March 2007, 10:59
If above example reports an error then I'm afraid I won't help you. It compiles on gcc without any errors. If the example works ok, and error occurs in some of your code then paste here exact error message and a piece of code that generates it (with a few lines above and below the line reported in error).