Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33

Thread: how to emit signal in a static function ?

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

    Default Re: how to emit signal in a static function ?

    yes, .it works well.

  2. #22
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to emit signal in a static function ?

    Quote Originally Posted by cxl2253 View Post
    yes, .it works well.
    Can we see the code? Based on my knowledge you can't rely on "this" pointer in a static method as the static method is not part of any object...

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

    Default Re: how to emit signal in a static function ?

    of course,the following is code.but i still cannot decalre the static variable in class scope,when i do it ,the VC7 always report various error.either not define the variable or cannot be interped by metaobject.
    Qt Code:
    1. ///////////////////////storescpThread.h
    2. typedef (*STORESCPCALLBACK)(void *callbackData, char* statusDetail);
    3. static unsigned long classAddress;
    4. class storescpThread:public QThread
    5. {
    6. Q_OBJECT
    7. public:
    8. storescpThread(QTextEdit *txtEdit,QObject *parent = 0)
    9. ~storescpThread();
    10. protected:
    11. void run();
    12. static void emitMsg( unsigned long user_value,char * filename)
    13. {
    14. storescpThread* pthis = (storescpThread*)user_value; //
    15. pthis->setText(state,filename);
    16. }
    17. void mycallback();
    18. private :
    19. QTextEdit * textEdit;
    20. static void storeSCPCallback(void *callbackData, char* statusDetail);
    21. void setText(char * state,char * name);
    22. };
    23.  
    24. ///////////////////////storeScpThread.cpp
    25. storescpThread::storescpThread(QTextEdit *txtEdit,QObject *parent): QThread(parent)
    26. {
    27. textEdit=txtEdit;
    28. classAddress =(unsigned long)this ;
    29. }
    30. void storescpThread:: storeSCPCallback(void *callbackData, char* statusDetail)
    31. {
    32. emitMsg(classAddress,statusDetail);
    33. return ;
    34. }
    35. void storescpThread::mycallback()
    36. {
    37. myThirdCallFunction(storeSCPCallback); //the third party function, which used callback as param
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

  4. #24
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to emit signal in a static function ?

    Oh God.... This is some kind of madness... I won't even ask what happens if you have two instances of the thread class or what happens if you delete the object without unregistering the callback...

    Why don't you move the callback method outside the class so that it's a standalone function instead? You can't call any of the nonstatic class member anyway, so what's the point of having this static method part of the thread class?

  5. #25
    Join Date
    Mar 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to emit signal in a static function ?

    Implementing some Win32 Callback is quite necessary in some cases to make *good* use of Win32 API.

    Found this old thread on google. What I did is to add a global pointer to the current class, like this:

    in the header file
    Qt Code:
    1. class MyClass {
    2. public:
    3. MyClass ();
    4. ~MyClass ();
    5. private:
    6. static void CALLBACK MyCallback (int nMsg);
    7. void emitMySignal (int nMsg);
    8. signals:
    9. mySignal (int nMsg);
    10. }
    To copy to clipboard, switch view to plain text mode 

    in the cpp file, at the beginning define a global pointer to the current class
    Qt Code:
    1. MyClass* pThis;
    2.  
    3. MyClass:MyClass ()
    4. {
    5. pThis = this;
    6. }
    7.  
    8. void MyClass::MyCallback (int nMsg)
    9. {
    10. pThis->emitMySignal(nMsg);
    11. }
    12.  
    13. void MyClass::emitMySignal (int nMsg)
    14. {
    15. emit mySignal (nMsg);
    16. }
    To copy to clipboard, switch view to plain text mode 

  6. #26
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to emit signal in a static function ?

    I had a very similar situation with the libusb library. If I wanted to have callbacks and use the async model they have laid out I couldn't figure out a better way.

    Is this the best way still, at least when using existing c libraries?

  7. #27
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: how to emit signal in a static function ?

    In my experience with callbacks from various libraries, there's usually a method for storing a pointer or user data that is presented to the callback. I've used that capability in the past to pass a pointer to the class instance, so that even though the static member callback function doesn't have a "this" pointer, the method would be able to execute any class functions or class data, etc.
    Last edited by anda_skoa; 7th July 2016 at 17:22.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  8. #28
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to emit signal in a static function ?

    Quote Originally Posted by jefftee View Post
    In my experience with callbacks from various libraries, there's usually a method for storing a pointer or user data that is presented to the callback. I've used that capability in the past to pass a pointer to the class instance, so that even though the static member callback function doesn't have a "this" pointer, the method would be able to execute any public class functions or public class data, etc.
    So basically sharing the class instance pointer to the static function? Then just making sure it is only used within a class instance and dealing with removing the reference with what ever means the library offers.

  9. #29
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: how to emit signal in a static function ?

    Quote Originally Posted by Goddard View Post
    So basically sharing the class instance pointer to the static function? Then just making sure it is only used within a class instance and dealing with removing the reference with what ever means the library offers.
    Easier to give you an example. Here's what I've used for libCurl when I've used its callback functions.

    header file that declares the static member function:

    Qt Code:
    1. private:
    2. static size_t write_function( void *ptr, size_t size, size_t nmemb, void *stream);
    To copy to clipboard, switch view to plain text mode 
    cpp file that implements the static member functiion:

    Qt Code:
    1. size_t libFlickr::write_function( void *ptr, size_t size, size_t nmemb, void *stream)
    2. {
    3. size_t total = size * nmemb;
    4.  
    5. libFlickr* pFlickr = reinterpret_cast<libFlickr*>(stream);
    6.  
    7. if (pFlickr)
    8. {
    9. pFlickr->some_public_method();
    10. return total;
    11. }
    12. else
    13. {
    14. return 0;
    15. }
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 
    and finally the cpp code that invokes libcurl, specifies the callback, and sets the user data pointer, etc:

    Qt Code:
    1. m_code = curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, libFlickr::write_function);
    2. m_code = curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, (void*)this);
    3. m_code = curl_easy_perform(m_pCurl);
    To copy to clipboard, switch view to plain text mode 
    In the case of libcurl, it passes the CURLOPT_WRITEDATA value to the callback function as the void* stream argument. Every other use I've had for callback functions has provided a way to pass data that is given back to the callback function, so passing the "this" pointer results in the callback receiving the instance pointer.

    Hope that helps.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  10. The following user says thank you to jefftee for this useful post:

    Goddard (7th July 2016)

  11. #30
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to emit signal in a static function ?

    Quote Originally Posted by jefftee View Post
    the method would be able to execute any public class functions or public class data, etc.
    Any method or data, even private.
    The static method is in the same class context.

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    jefftee (7th July 2016)

  13. #31
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: how to emit signal in a static function ?

    Quote Originally Posted by anda_skoa View Post
    Any method or data, even private.
    The static method is in the same class context.
    Thanks, I'll edit my post so that I don't mislead anyone that runs across this thread on the future.

    Edit: For some reason, I don't see an option to edit my prior post...
    Last edited by jefftee; 7th July 2016 at 16:29.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  14. #32
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to emit signal in a static function ?

    Strange, I edited it for you.

    Cheers,
    _

  15. The following user says thank you to anda_skoa for this useful post:

    jefftee (8th July 2016)

  16. #33
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to emit signal in a static function ?

    Good stuff. I'm just learning working with this type of library. Appreciate that. I now noticed it does indeed offer a void* user_data parameter in the function to pass through. I just overlooked that completely.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. From extends QTreeWidgetItem emit signal?
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2006, 14:54
  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. emit a signal
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 11:14

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.