Results 1 to 20 of 22

Thread: Problem emitting signal from a static function

Hybrid View

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

    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.

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

    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?

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

    Default Re: Problem emitting signal from a static function

    Does that function do any other stuff?

    Regards

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

    Default Re: Problem emitting signal from a static function

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

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

    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?

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

    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.

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

    Default Re: Problem emitting signal from a static function

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

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

    Valheru (12th June 2007)

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

    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

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.

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

    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.

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem emitting signal from a static function

    In Java you have to do the synchronisation as well of course... It's not as simple as calling any function doing anything you want anywhere you want.

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

    Default Re: Problem emitting signal from a static function

    Sorry I didn't update this thread sooner. I did indeed solve it by subclassing QCustomEvent. In the end the solution was simple to implement, and works very well. Thank you all for your help and patience.

    /edit : the crash was being caused by accessing a function running in another thread directly. It was solved by bundling the data in the subclassed QCustomEvent as suggested.

Similar Threads

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