PDA

View Full Version : QThread trying to limit threads



spawn9997
27th August 2009, 20:48
I have a function in my code that takes a list of points () from a QMap with key 'linekey'. Linekey is a QString describing which 'Line' I want to get from the map. My code is as follows:


foreach (const QString linekey, linekeys) {

if (groupValues.contains(linekey))
continue;
QList<ReceiverGraphicsItem *> receiverlist = lineGroup.values(linekey);
if (lod <= .03) {
points2[0] = mapToPix(receiverlist.at(0)->pos());
points2[1] = mapToPix(receiverlist.at(receiverlist.size()/4)->pos());
points2[2] = mapToPix(receiverlist.at((receiverlist.size()*2)/4)->pos());
points2[3] = mapToPix(receiverlist.at((receiverlist.size()*3)/4)->pos());
points2[4] = mapToPix(receiverlist.at((receiverlist.size()-1))->pos());
ptrr.drawPolyline(points2,small);
} else {
points[0] = mapToPix(receiverlist.at(0)->pos());
points[1] = mapToPix(receiverlist.at(receiverlist.size()/14)->pos());
points[2] = mapToPix(receiverlist.at((receiverlist.size()*2)/14)->pos());
points[3] = mapToPix(receiverlist.at((receiverlist.size()*3)/14)->pos());
points[4] = mapToPix(receiverlist.at((receiverlist.size()*4)/14)->pos());
points[5] = mapToPix(receiverlist.at((receiverlist.size()*5)/14)->pos());
points[6] = mapToPix(receiverlist.at((receiverlist.size()*6)/14)->pos());
points[7] = mapToPix(receiverlist.at((receiverlist.size()*7)/14)->pos());
points[8] = mapToPix(receiverlist.at((receiverlist.size()*8)/14)->pos());
points[9] = mapToPix(receiverlist.at((receiverlist.size()*9)/14)->pos());
points[10] = mapToPix(receiverlist.at((receiverlist.size()*10)/14)->pos());
points[11] = mapToPix(receiverlist.at((receiverlist.size()*11)/14)->pos());
points[12] = mapToPix(receiverlist.at((receiverlist.size()*12)/14)->pos());
points[13] = mapToPix(receiverlist.at((receiverlist.size()*13)/14)->pos());
points[14] = mapToPix(receiverlist.at((receiverlist.size()-1))->pos());
ptrr.drawPolyline(points,big);
}
} The ptrr is a QPainter that is connected to a QPixmap (QImage in threaded version!).

This works fine but is slow with hundreds of lines to draw; thus the idea is to use Threads. if I use the loop method directly as it was but in a thread class then I get hundreds of threads; My question is, how do I make it so that only x number of threads run at one time? I have heard of a queue but have not seen one example of it. Maybe there is a better way to split up this job to use threading?


thank you,

JW
sw developer

"Some days you just can't get rid of a bomb." Batman movie - 1966

wysota
27th August 2009, 21:34
Read about QtConcurrent.

spawn9997
2nd September 2009, 15:51
Thanks, but after reading about QtConcurrent and other articles you have posted on threading. I decided another approach would be better serving. I now have 2 threads for the two items of concern and they do all of the work in the background after I put up a temp pixmap.


JW
sw developer