Results 1 to 7 of 7

Thread: how to define the callback function in QThread?

  1. #1
    Join Date
    Dec 2006
    Posts
    36
    Thanked 1 Time in 1 Post

    Default how to define the callback function in QThread?

    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 ublic 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 ublic QThread
    {
    Q_Object
    private:
    static void storeSCPCallback(char* progress, char * filename);
    protected:
    void myfuncion(STORESCPCALLBACK test);
    void run();
    }

  2. #2
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to define the callback function in QThread?

    Quote Originally Posted by cxl2253 View Post
    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:
    Qt Code:
    1. // MyClass.h
    2. class MyClass {
    3. static int myStatic; // this is only declaration
    4. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // MyClass.cpp
    2. #include "MyClass.h"
    3.  
    4. int MyClass::myStatic = 0; // here is definition
    To copy to clipboard, switch view to plain text mode 
    The Wheel weaves as the Wheel wills.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to define the callback function in QThread?

    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

  4. #4
    Join Date
    Dec 2006
    Posts
    36
    Thanked 1 Time in 1 Post

    Default Re: how to define the callback function in QThread?

    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;
    Last edited by cxl2253; 28th March 2007 at 01:56.

  5. #5
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to define the callback function in QThread?

    Quote Originally Posted by cxl2253 View Post
    Qt Code:
    1. typedef void (__stdcall * STORESCUCALLBACK)(T_StoreProgress* progress);
    2. class myclass
    3. {
    4. static STORESCUCALLBACK mystorecallback;
    5. }
    6. STORESCUCALLBACK myclass::mystorecallback=NULL;
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. // Test.h
    2. #ifndef TEST_H_
    3. #define TEST_H_
    4.  
    5. typedef int (* FUNC_PTR)(int, int);
    6.  
    7. class Test
    8. {
    9. public:
    10. static int add(int l, int r);
    11. static int mul(int l, int r);
    12. static FUNC_PTR funcPtr; // declaration of static pointer to function
    13.  
    14. void doTheJob();
    15.  
    16. private:
    17. int callback(FUNC_PTR f, int l, int r); // declaration of function with parameter
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // Test.cpp
    2. #include "Test.h"
    3. #include <iostream>
    4.  
    5. FUNC_PTR
    6. Test::funcPtr = NULL; // definition of static pointer to function
    7.  
    8. int
    9. Test::add(int l, int r) {
    10. return l+r;
    11. }
    12.  
    13. int
    14. Test::mul(int l, int r) {
    15. return l*r;
    16. }
    17.  
    18. int
    19. Test::callback(FUNC_PTR f, int l, int r) {
    20. return f(l, r);
    21. }
    22.  
    23. void
    24. Test::doTheJob() {
    25. int result = callback(funcPtr, 2, 5);
    26. std::cout << "result = " << result << std::endl;
    27. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // main.cpp
    2. #include "Test.h"
    3. int main ()
    4. {
    5. Test test;
    6. test.funcPtr = Test::add;
    7. test.doTheJob();
    8.  
    9. test.funcPtr = Test::mul;
    10. test.doTheJob();
    11. }
    To copy to clipboard, switch view to plain text mode 
    The Wheel weaves as the Wheel wills.

  6. #6
    Join Date
    Dec 2006
    Posts
    36
    Thanked 1 Time in 1 Post

    Default Re: how to define the callback function in QThread?

    but if declare static variable in class,it will report error.

  7. #7
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to define the callback function in QThread?

    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).
    The Wheel weaves as the Wheel wills.

Similar Threads

  1. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.