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:
    @
    ther is class Finder

    QWaitCondition wCond;
    QMutex sync;
    QImage img;
    void imageRecieved(QImage screenshot);
    signals:
    void needScreenshot();

    void Finder::wait()
    {
    QTest::qSleep(50);
    sync.lock();
    emit needScreenshot();
    wCond.wait(&sync);
    sync.unlock();
    }
    void Finder::imageRecieved(QImage screenshot)
    {
    img = screenshot;
    wCond.wakeOne();
    }
    @

    @
    in main gui thread

    Finder* finder;

    QObject::connect(finder, SIGNAL(needScreenshot()),SLOT(makeScreeshot()));

    void makeScreeshot()
    {
    unsigned long windowId;
    windowId=sc.getHeroesWinId(mainWinForm_.lineEdit->text());//returns window id depends on system, i can list it's code if needed
    if(windowId == 0)
    {
    QImage img;
    finder->imageRecieved(img);
    return;
    }
    QPixmap pixmap = QPixmap::grabWindow((WId)windowId);
    QImage img(pixmap.toImage());
    finder->imageRecieved(img);
    }
    @



  • I don't know what exactly is your problem, but

    I would use QMutexLocker lock(&sync); in wait().

    In imageRecieved() I would do the same:

    @
    void Finder::imageRecieved(QImage screenshot)
    {
    QMutexLocker lock(&sync);
    img = screenshot;
    wCond.wakeOne();
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.