views:

167

answers:

1

given a stringlist containing 4 names. I need to call qprocess for each name in loop. To avoid freezing I am using qthread as suggested in this forum.

given that (say):

QStringList queueList contains 4 elements all.q, a1.q, a2.q, a3.q

for(int z= 0; z < queueList.length(); z++) { // for each z call a thread that in turn will start a process to run an external // command (qconf -sq all.q) }

Brgds,

knish ( growing in qt )

+1  A: 

Try something like this. QtConcurrent will optimize the thread count.

void executeInProcess(QString& text)
{
    QProcess::execute( "qconf", QStringList() << "-sq" <<  text);
}

void main()
{

    QApplication app;
    MainWindow mainWindow;
    //...
    QStringList queueList;
    QFutureWatcher watcher; 
    connect(&watcher, SIGNAL(finished()), &mainWindow, SLOT(whatEverYouWantToDo()));
    QFuture<void> result = QtConcurrent::map(queueList, executeInProcess);
    watcher.setFuture(result);
    //...
    app.exec();
}


Edit

If you want result from every process you need the mapped function QFuture<T> mapped ( const Sequence & sequence, MapFunction function ) and executeInProcess needs to return the result from the QProcess call.

QString executeInProcess(QString& text)
{
    QString result;
    QProcess::execute( "qconf", QStringList() << "-sq" <<  text);
    // ... 
    return 
}

in whatEverYouWantToDo() you can iterate over the results

QFuture<QString> result ;
QFutureIterator<QString> i(result);
while (i.hasNext()) {    
    qDebug() << i.next();
}
TimW
From the code you have suggested, this is what i understand. kindly crosscheck1) QStringList queueList; contains all.q, a1.q, a2.q, a3.q such that when the line QFuture<void> result = QtConcurrent::map(queueList, executeInProcess); runs it will run for all the items in the queueList and whatever I need to do with the result of each executeInProcess, I need to mention that in whatEverYouWantToDo() function. In other words, the code you have mentioned here will completely take care of the looping thru the queueList without a for loop. Kindly ascertain.2)
knishua
Hi Tim, I tried the code u suggested on top. QFuture gives an output after all the items in the sequence of QtConcurrent::map(sequenceList,functionF) is exhausted. However, what i need is that, within each call to the functionF, that I, be able to start a process, get its result in a file, modify it, save the file and use another process to throw it back into the system. Here I would need a class that will ensure that only after one process has completed that the following ones will follow
knishua
@knishua What do you need? You have a list of strings and want to processed the items sequential or parallel? What do you mean by throw it back to the process?
TimW
Tim, i have a list of string and want to process it such thata) functionF will call external command qprocess:start("qconf -sq listL[i]") b) the output of this will be stored in /tmp/tempfile.txt c) this file is modified and saved d) functionF calls external comm "qconf -Mq /tmp/tempfile.txt" at present step d should start only after step a b and c have completed. :)proc->setStandardOutputFile("/tmp/tempfile.txt") is used to set the output
knishua