[Interest] QThread and QPixmap::grabWindow()

Alex Malyushytskyy alexmalvtk at gmail.com
Thu Aug 8 01:21:10 CEST 2013


As far as I understand you need to make sure that window is grabbed in the
main thread.
So the clean way to do it -  do it in the main threat without changing
affinity .
and wait until picture is taken in the threat you need a picture at.

This will also remove race conditions you are facing - your

grabWindow might be called from different threads and more of that it
might be called at the same time as painting occur, which should not
happen.

You can't avoid synchronization here. It is your choice through events.
mutexes or signal/slots . I would personally prefer last with
BlockingQueuedConnection.

Regards,
      Alex



On Wed, Aug 7, 2013 at 3:11 PM, Alexander Syvak <alexander.svk at gmail.com>wrote:

> Hello,
>
> there's a need for each thread to make screenshots and to compare them
> with loaded picture using QImage.
> Since there's only on GUI thread, I moved the the thread context to the
> main thread to make the screenshot in there.
>
> Here's the code snipper from the worker-threads:
>
>        QPixmap screenshot;
>
>     try
>
>     {
>
>         auto const this_thread = this->thread();
>
>         moveToThread(QApplication::instance()->thread());
>
>         screenshot = QPixmap::grabWindow( QApplication::desktop()->winId() );
>
>         moveToThread(this_thread);
>
>         screenshots.append(screenshot);
>
>         // Compare it with the trigger if there was no match before.
>
>         if ( !matched && event_p && event_p->get_trigger_path().size() && images_comparator::cmp( screenshot.toImage(), QImage( event_p->get_trigger_path() ), pattern() ) )
>
>         {
>
>             qDebug() << "!!! matched !!!";
>
>             matched = true;
>
>
> However, the grabWindow returns null QPixmap always.
>
> Who knows how to achive what's needed here?
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130807/169e36ba/attachment.html>


More information about the Interest mailing list