Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Problem emitting signal from a static function

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem emitting signal from a static function

    I am writing a piece of code that handles callback from a C-library I link against. This library uses a callback function to show progress in a decoding run. I am using a static member function in a thread to act as the callback function, and I want to emit a signal from it to inform the GUI thread of the decoding progress. However, I can't get it to compile and I can't seem to find anyone who solved a similar problem. The error I am getting is:

    Qt Code:
    1. /home/valheru/svn/knewz/src/decoder.cc: In static member function ‘static int Decoder::BusyCallback(void*, uuprogress*)’:
    2. /home/valheru/svn/knewz/src/decoder.cc:40: error: cannot call member function ‘void Decoder::progress(const int&)’ without object
    To copy to clipboard, switch view to plain text mode 

    Does anyone know a workaround for this, or is it simply impossible to emit signals from static member of a class?

  2. #2
    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: Problem emitting signal from a static function

    Oh boy,this issue come time and again.
    Have a look here
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem emitting signal from a static function

    Heh, I just finished reading that trainwreck >_<

    But the problem remains, unfortunately. I HAVE to use a callback, since I am linking against the uulib library, which is about the best cross-platform library there is for decoding Usenet part-files. So short of writing my own (which I won't) I have to use it's callback function. Now, perhaps I could do something with a QEvent, since the callback function does not necessarily have to be a static member function. However, it still cannot emit a signal in that case, since a signal has to be emitted from an object. Still, a QEvent and a QApplication:ostEvent() may work, but I am not sure how to get it to work, since it would still need an object (the GUI object) to be passed to as the first parameter of postEvent().

  4. #4
    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: Problem emitting signal from a static function

    Heh, I just finished reading that trainwreck
    Well, the general solutions how to tranfer objects through callback (and thus be able to emit a signal of passed object) is in this post
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem emitting signal from a static function

    Ugh, how he solved it isn't really applicable in my case. The callback is called every 100 milliseconds, and the overhead of creating an object, setting up the signals and connecting the to the GUI threads slots is an excessive amount of overhead I feel, for such a simple thing. I appear to be getting somewhere with the QApplication::postEvent() approach - it's compiling at least However, I need to find out how to process the event in the GUI thread, and at the moment if I set up the QEvent and postEvent() it then the decoder never gets around to decoding the files, which is weird since psotEvent() just fires the
    QEvent off and returns immediately according to the documentation

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem emitting signal from a static function

    Does the receiver reimplement QObject::customEvent() to catch the custom event? Is the event type larger or equal to QEvent::User?
    J-P Nurmi

  7. #7
    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: Problem emitting signal from a static function

    how he solved it isn't really applicable in my case
    and
    and the overhead of creating an object, setting up the signals and connecting the to the GUI threads slots is an excessive amount of overhead I feel
    Have nothing in common.
    Here is the code:
    Qt Code:
    1. static void emitMsg( unsigned long user_value,char * filename)
    2. {
    3. myclass* pthis = (myclass*)user_value; // get this address
    4. pthis->emit storescpProgressInfo(filename) ;
    5. }
    To copy to clipboard, switch view to plain text mode 

    No allocation, no setting up connections, no nothing, just emiting a signal.
    You set the connection there where the emitting and receiving object are visible, and you only need to do it once.
    But then again, I don't really know what it is you are trying to do (appart from emiting a signal in a callback.).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem emitting signal from a static function

    Quote Originally Posted by jpn View Post
    Does the receiver reimplement QObject::customEvent() to catch the custom event? Is the event type larger or equal to QEvent::User?
    No, I hadn't set it up yet but I was busy doing so. I thought I would just fire off the event and made sure I was on the right track. Didn't expect it to stop the decoder working...
    I am using QEvent::User as the event type though.

  9. #9
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem emitting signal from a static function

    Quote Originally Posted by high_flyer View Post
    and


    Have nothing in common.
    Here is the code:
    Qt Code:
    1. static void emitMsg( unsigned long user_value,char * filename)
    2. {
    3. myclass* pthis = (myclass*)user_value; // get this address
    4. pthis->emit storescpProgressInfo(filename) ;
    5. }
    To copy to clipboard, switch view to plain text mode 
    No allocation, no setting up connections, no nothing, just emiting a signal.
    You set the connection there where the emitting and receiving object are visible, and you only need to do it once.
    But then again, I don't really know what it is you are trying to do (appart from emiting a signal in a callback.).
    You are right, the slots would only be set up when the decoder si initially created. However, it seems a bit messy. If the way I'm trying to do it now doesn't work I'll give it a shot.

  10. #10
    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: Problem emitting signal from a static function

    But what is the way you are trying?
    I am not clear on how you want to integrate QEvent in the external library?
    Is it an open source lib?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem emitting signal from a static function

    Was my explanation that bad?

    I am decoding files in a QThread. I am doing that using an open-source library, called uulib. It is THE library for decoding Usenet multipart files, which is what my program does. Well, it downloads them, but you get the picture. Now, rather than go through all the hassle of reinventing the wheel, I link against uulib and let it decode the files for me that my program has downloaded. However, uulib is a C library, and uses function callbacks to do certain things, one of which is to display it's progress during decoding. I would like to set up a function callback, and then do some calculations in it to determine the progress so far, and then to fire off a signal to the GUI thread so that it can update a progress bar with the progress of the decoder. Hence my whole problem

  12. #12
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem emitting signal from a static function

    Quote Originally Posted by Valheru View Post
    No, I hadn't set it up yet but I was busy doing so. I thought I would just fire off the event and made sure I was on the right track. Didn't expect it to stop the decoder working...
    I am using QEvent::User as the event type though.

    Ok, that sucks. When the event is caught in the main GUI thread, I then access the other thread using a public function to get the current progress. however, this causes the program to crash immediately. Is it forbidden to access QThreads this way? In Java I can simply use a public function with no ill effects between threads - is that possible with QThreads? Or should I set up some round-robbin thread/slot implementation?

  13. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem emitting signal from a static function

    Does that function do any other stuff?

    Regards

  14. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem emitting signal from a static function

    Why not bundle the progress into the QEvent object?
    J-P Nurmi

  15. #15
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem emitting signal from a static function

    Quote Originally Posted by jpn View Post
    Why not bundle the progress into the QEvent object?
    I don't know how The docs don't mention it either as far as I can see, unless you mean by subclassing it?

  16. #16
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem emitting signal from a static function

    Yes, of course you have to create a custom event.
    Don't forget to give it an id greater or equal that QEvent::User.

    You can add some extra members to your subclass to hold the progress data and whatever you may need.

  17. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem emitting signal from a static function

    See the attached example.
    Attached Files Attached Files
    J-P Nurmi

  18. The following user says thank you to jpn for this useful post:

    Valheru (12th June 2007)

  19. #18
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem emitting signal from a static function

    I'm not sure if a solution has been found yet, but maybe you could simply keep a single global object to which to connect signals/slots. This could be a global variable or a singleton object or something similar. You can control that object from your static function.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  20. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Problem emitting signal from a static function

    I think that was already suggested in the other thread. From what I've read here, the solution with posting a custom event I find most appealing as you don't need a "sender" object for that.

    Using a global object should work but at the same time violates some rules of good design, while the solution using events directly seems to be ok.

  21. #20
    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: Problem emitting signal from a static function

    Was my explanation that bad?
    Probably not, but since I am doing it "along side" my work, my concentration can span so much
    In Java I can simply use a public function with no ill effects between threads - is that possible with QThreads? Or should I set up some round-robbin thread/slot implementation?
    Well, in C++ you have to make sure the function is thread safe - that means - that there is no situation where two threads are trying to access the same reasorce at the same time.
    You should use mutex or semaphores to protect the mutual excluded code.

    I too vote for the custom event solution, its the "cleanest".
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 21:36
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. Replies: 3
    Last Post: 15th April 2007, 19:16
  4. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  5. 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

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.