yazwas
2nd June 2010, 13:00
Good day All
I have this issue, that I want to make sure how to solve it
I use QtConcurrent::run to run a function in the background.
This function is run multiple times (it takes a while to complete executing, it saves image to network drive and then updates the database), and the next call to this function can be issued while the first call is still executing.
I also pass a database object to this function. I want to know if this is a correct thing to do or not. the problem is that it used to run OK under Windows, but under Linux, it keeps crashing whenever the next call to this function is issued.
here is my code
QFuture<bool> future = QtConcurrent::run(&saveImages, imagesList, db);
saveImagesWatcher.setFuture(future);
This is the saveImages function, its a static function
bool MainWindow::saveImages(QList<QImage> &list, QSqlDatabase &database)
{
QString path = /path/to/drive
QDir dir;
if( !dir.exists(path) )
if( !dir.mkpath(path) )
return false;
bool ret = true;
int i = 0;
int id_article = 10; //set up somewhere else
for(i=0; i< list.count(); i++) //this could take a lof of time. depending on the images number
{
QString fname = QString("%1-%2.jpg").arg(id_article).arg(i+1);
if( file.exists(path + fname) )
file.remove(path + fname);
ret = list[i].save(path + fname, "jpg", 100);
if(!ret)
{
qDebug() << "saveImages(): Saving image " << fname << " Unsuccessfull";
ret = false;
break;
}
}
if(!ret)
{
deleteEntry(database, id_article);
return false;
}
qDebug("saveImages(): Everything is OK for article %d, updating the status and images number", id_article);
updateStatus(database, id_article, 2);
updateImagesNumber(database, id_article, list.count());
return true;
}
I want to know what is the best way to run my function in a multiple threads. and should I pass the database object to it or not. if I should use QThread, how can I make multiple calls to the same function, i.e. how can I create multiple thread objects (they should be created every time the function gets called) and run them.
Best regards
I have this issue, that I want to make sure how to solve it
I use QtConcurrent::run to run a function in the background.
This function is run multiple times (it takes a while to complete executing, it saves image to network drive and then updates the database), and the next call to this function can be issued while the first call is still executing.
I also pass a database object to this function. I want to know if this is a correct thing to do or not. the problem is that it used to run OK under Windows, but under Linux, it keeps crashing whenever the next call to this function is issued.
here is my code
QFuture<bool> future = QtConcurrent::run(&saveImages, imagesList, db);
saveImagesWatcher.setFuture(future);
This is the saveImages function, its a static function
bool MainWindow::saveImages(QList<QImage> &list, QSqlDatabase &database)
{
QString path = /path/to/drive
QDir dir;
if( !dir.exists(path) )
if( !dir.mkpath(path) )
return false;
bool ret = true;
int i = 0;
int id_article = 10; //set up somewhere else
for(i=0; i< list.count(); i++) //this could take a lof of time. depending on the images number
{
QString fname = QString("%1-%2.jpg").arg(id_article).arg(i+1);
if( file.exists(path + fname) )
file.remove(path + fname);
ret = list[i].save(path + fname, "jpg", 100);
if(!ret)
{
qDebug() << "saveImages(): Saving image " << fname << " Unsuccessfull";
ret = false;
break;
}
}
if(!ret)
{
deleteEntry(database, id_article);
return false;
}
qDebug("saveImages(): Everything is OK for article %d, updating the status and images number", id_article);
updateStatus(database, id_article, 2);
updateImagesNumber(database, id_article, list.count());
return true;
}
I want to know what is the best way to run my function in a multiple threads. and should I pass the database object to it or not. if I should use QThread, how can I make multiple calls to the same function, i.e. how can I create multiple thread objects (they should be created every time the function gets called) and run them.
Best regards