January 3, 2011

yvvan yvvan
Lab Rat
1 posts

Something wrong with QWaitCondition

 

For a long time i compiled my program only on Linux with qt 4.6.2. But then decided to watch it on my windows xp using qt creator with qt 4.7.0. What happened: on linux every signal was sent to gui thread and waked second thread, but in windows it (sending signal) worked only for 1-4 times and then second thread went to wait condition forever.
I have main gui thread and second thread, that calls wait() function for lots of times.

in my second thread:

  1. ther is class Finder
  2.  
  3. QMutex sync;
  4. QImage img;
  5. void imageRecieved(QImage screenshot);
  6. signals:
  7.     void needScreenshot();
  8.  
  9. void Finder::wait()
  10. {
  11.     QTest::qSleep(50);
  12.     sync.lock();
  13.     emit needScreenshot();
  14.     wCond.wait(&sync);
  15.     sync.unlock();
  16. }
  17. void Finder::imageRecieved(QImage screenshot)
  18. {
  19.     img = screenshot;
  20.     wCond.wakeOne();
  21. }

  1. in main gui thread
  2.  
  3. Finder* finder;
  4.  
  5. QObject::connect(finder, SIGNAL(needScreenshot()),SLOT(makeScreeshot()));
  6.  
  7. void makeScreeshot()
  8. {
  9.     unsigned long windowId;
  10.     windowId=sc.getHeroesWinId(mainWinForm_.lineEdit->text());//returns window id depends on system, i can list it's code if needed
  11.     if(windowId == 0)
  12.     {
  13.         QImage img;
  14.         finder->imageRecieved(img);
  15.         return;
  16.     }
  17.     QPixmap pixmap = QPixmap::grabWindow((WId)windowId);
  18.     QImage img(pixmap.toImage());
  19.     finder->imageRecieved(img);
  20. }

1 reply

January 3, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

I don’t know what exactly is your problem, but

  1. I would use QMutexLocker lock(&sync); in wait().
  2. In imageRecieved() I would do the same:
    1. void Finder::imageRecieved(QImage screenshot)
    2. {
    3.     QMutexLocker lock(&sync);
    4.     img = screenshot;
    5.     wCond.wakeOne();
    6. }
 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

 
  ‹‹ [Solved] Restriction when making modal mdi windows      QMediaPlayer / QVideoWidget makes QX11EmbedContainer disappear ››

You must log in to post a reply. Not a member yet? Register here!