Results 1 to 8 of 8

Thread: Tangled in QThreads

  1. #1
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Tangled in QThreads

    I have an issue similar to this link http://www.qtcentre.org/forum/f-qt-p...hlight=QThread

    I have tried to use the QtConcurrent::run function but it seems something is still wrong.
    What I have is a long operation that calculates points that go on a map (QImage or QPixmap). I want the result to be a point list (QPolygonF) such that I can call QPainter::drawPoints(const QPolygonF & points). Now this works fine when I have 1 set of data. I want to split the job and get it done quicker by using say 2 (or more) calls to QtConcurrent::run on a part of the data.
    The idea is to call them one after the other then wait for all of them to be done. For each one that finishes I can draw that polygon to the map; and, when done I have the full map.
    What happens is I get one of them to finish but am baffled as to how to wait for all of the functions to return. I tried sub classing QThread but it seems when one instance of the thread is called the other returns immediately. Can I only call one instance of a sub class of QThread at a time?

    Will provide code if needed.

    JW
    sw developer

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Tangled in QThreads

    QtConcurrent is supposed to be a higher level API that relieves you from having to work with QThread directly. How/why do you mix both?

    Best show us some code, so we have a proper grounds for discussion.

  3. #3
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Tangled in QThreads

    Oh sorry, caduel, I have confused you. I first tried with QtConcurrent then I tried with QThread. Let me show you some code...

    Here is the line that will call the function:
    Qt Code:
    1. QtConcurrent::run(this,&MidpointSceneThread::getMidpoints,myStruct)
    To copy to clipboard, switch view to plain text mode 

    where myStruct is a bunch of data I need in the function.
    defined as:

    Qt Code:
    1. struct MidpointStruct {
    2. QList<ShotGraphicsItem *> sgiList;
    3. GeometryVersion *gvLocal;
    4. int startIndex;
    5. int size;
    6. int decimateBy;
    7. QImage image;
    8. MidpointGraphicsLayer * mpgl;
    9.  
    10. };
    To copy to clipboard, switch view to plain text mode 

    ShotGraphicsItem and MidpointGraphicsLayer are types of QGraphicsItems,


    declaration of function (it is a part of the class MidpointSceneThread):
    Qt Code:
    1. QPolygonF MidpointSceneThread::getMidpoints(MidpointStruct &myStruct);
    To copy to clipboard, switch view to plain text mode 

    This function goes through the list of ShotGraphicsItems and does some calculations and returns a set of points (Midpoints) that I use to draw on a pixmap.

    The issue is I want the list of ShotGraphicsItem to be broken up and I tried several things to get the future return items...


    I tried making a loop and calling the run function 3 times...
    More CODE:
    Qt Code:
    1. for (int ndx = 0; ndx < numThreads; ++ndx){
    2. QFuture<QPolygonF> midpoints[ndx] = QtConcurrent::run(this,&MidpointSceneThread::getMidpoints,myStruct[ndx]);
    3. }
    To copy to clipboard, switch view to plain text mode 

    Is this even a proper way to split up the job? when I run this and call :
    Qt Code:
    1. for (int ndx = 0; ndx < numThreads; ++ndx)
    2. resultPoly[ndx] = midpoints[ndx].result();
    To copy to clipboard, switch view to plain text mode 

    I get the first one but the loop does not get the other 2 results and only part of the map is drawn.

    I hope this makes it clearer what I'm asking.


    JW

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Tangled in QThreads

    have you had a look at QtConcurrent::mapped()?

  5. The following user says thank you to caduel for this useful post:

    spawn9997 (14th October 2009)

  6. #5
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Tangled in QThreads

    I have but I am in the middle of looking at it again. QtConcurrent::mapped() or QtConcurrent::blockingMapped() should work better than QtConcurrent::run. I see (not from documentation but from reading this forum) that the map style function must take 1 argument. Does that mean there is no way to use a class member function (method) with this? Is there a way around this?



    jw

  7. #6
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Tangled in QThreads

    Thanks for the reference. After studying my code I found an error that caused only one of my threads to loop fully. After fixing this error now all threads complete. I also found out about
    Qt Code:
    1. std::bind1st
    To copy to clipboard, switch view to plain text mode 
    which allows me to use my member function with the concurrent Map function.
    For all interested here is what I did:
    function declaration:
    Qt Code:
    1. QPolygonF MidpointSceneThread::partialDraw(MidpointStruct myStruct)
    To copy to clipboard, switch view to plain text mode 
    NOTE: You cannot use a reference type because of the bind function so I pass by value the struct 'myStruct'.

    Then I call it like this:

    Qt Code:
    1. QList<QPolygonF> polys = QtConcurrent::blockingMapped (structList,std::bind1st(std::mem_fun(&MidpointSceneThread::partialDraw),this));
    To copy to clipboard, switch view to plain text mode 

    when that returns I can use my List of polygons as I wish!!

    Last question... is the use of std:bind1st not portable? I'm not familiar with some of the std:: functions.


    JW
    sw developer

  8. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Tangled in QThreads

    have a look at Boost.Bind if you want to pass a (bound) member function;
    there should be some threads here on how to use it, search for "boost::bind"

  9. #8
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Tangled in QThreads

    Thanks I'll do that.

Similar Threads

  1. Counter-intuitive behavior of QThreads signals/slots
    By reddish in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2008, 20:40
  2. Identifying QThreads
    By TheGrimace in forum Newbie
    Replies: 2
    Last Post: 18th March 2008, 16:39
  3. Qthreads and webcam app
    By Schizo in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2007, 09:17
  4. Qthreads
    By Sheetal in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2007, 11:12
  5. QTcpSockets and QThreads
    By TheRonin in forum Newbie
    Replies: 3
    Last Post: 21st June 2006, 09:41

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.