Results 1 to 3 of 3

Thread: Signal isn't catched in Thread

  1. #1
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Signal isn't catched in Thread

    I want to fire a signal to an Object that moved into a QThread, it doesn't receive fired "startFlash" signal. Below is my code:

    Qt Code:
    1. //mainwindow.cpp
    2. .....
    3. ThreadFlasher = new QThread;
    4. flasher = new Flasher(0);
    5. .....
    6. connect(ThreadFlasher, SIGNAL(started()), flasher, SLOT(process())); // This works, process function in flasher runs
    7. connect(this, SIGNAL(stopThread()), flasher, SLOT(stopThread()));
    8. connect(this, SIGNAL(startFlash()), flasher, SLOT(startFlash()));
    9. ...
    10.  
    11. void MainWindow::on_pushButtonFW_clicked()
    12. {
    13. emit startFlash();
    14. }
    15. void MainWindow::on_openButtonConnect_clicked()
    16. {
    17. flasher->moveToThread(ThreadFlasher);
    18. ThreadFlasher->start();
    19. }
    20.  
    21.  
    22. ....
    23. //Flasher.cpp
    24.  
    25. void Flasher::process()
    26. {
    27. while(run)
    28. {
    29. msleep(10); // I think There is not enough time to get variable startFlashing?
    30. if(startFlashing)
    31. {
    32. FLASH_StartFlash();
    33. startFlashing = false;
    34. }
    35. }
    36. emit finished();
    37. }
    38.  
    39. void Flasher::startFlash()
    40. {
    41. startFlashing = true;
    42. }
    43. void Flasher::stopThread()
    44. {
    45. run = false;
    46. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Signal isn't catched in Thread

    Ok then, I tell You how I solved this problem. I will explain to You what seems to be going on in Your code.
    When You start process() function which is a slot You block event loop in QThread because of while loop. I did the same.
    The solution is to use code whith QEventLoop and QTimer. Here is a loop You can try.

    Qt Code:
    1. void
    2. QTimer timer;
    3. timer.setSingleShot(true);
    4. QObject::connect(&timer, SIGNAL(timeout), &loop, SLOT(quit());
    5.  
    6. startLoop:
    7. {
    8. timer.start(20);
    9. loop.exec()
    10.  
    11.  
    12. // (...)
    13.  
    14.  
    15.  
    16. }
    17. if(run)
    18. {
    19. gotoLoop:
    20. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal isn't catched in Thread

    The solution is to not block the thread's event loop.

    This looks like there is no need for process() at all, this could just call FLASH_StartFlash() in startFlash()

    Cheers,
    _

Similar Threads

  1. ACE Thread and QT signal or event
    By opengllove in forum Newbie
    Replies: 1
    Last Post: 27th December 2013, 13:07
  2. Can't receive signal from another thread.
    By useryy in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2011, 12:41
  3. Replies: 9
    Last Post: 28th November 2009, 20:31
  4. get finished signal from a thread
    By ProTonS in forum Qt Programming
    Replies: 4
    Last Post: 21st August 2009, 14:17
  5. Can you send a signal to a thread?
    By Dumbledore in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2007, 20:31

Tags for this Thread

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.