Dear all,

I am working with legacy code in a class AddTileStackToPixmapcacheWorker with header (much simplified example):

Qt Code:
  1. #ifndef AddTileStackToPixmapcacheWorkerQRunnableQRUNNABLE_H__
  2. #define AddTileStackToPixmapcacheWorkerQRunnableQRUNNABLE_H__
  3.  
  4. #include <QObject>
  5. #include <QRunnable>
  6. #include <QGraphicsPixmapItem>
  7.  
  8. #include <boost/gil/extension/io/jpeg_io.hpp>
  9. #include <boost/gil/extension/io/png_io.hpp>
  10. #include <boost/gil/image.hpp>
  11.  
  12.  
  13. #include <set>
  14.  
  15.  
  16. class AddTileStackToPixmapcacheWorker : public QObject, public QRunnable {
  17.  
  18. Q_OBJECT
  19. public:
  20. AddTileStackToPixmapcacheWorker (QString Filename);
  21. ~AddTileStackToPixmapcacheWorker ();
  22.  
  23. void run();
  24. void LoadImagesFromHardDriveIntoVector();
  25. bool LoadImageDataRgb8(QString fileNameRelative, QPixmap & ima);
  26.  
  27. private:
  28. QString m_Filename;
  29. };
  30.  
  31. #endif
To copy to clipboard, switch view to plain text mode 


and cpp:
Qt Code:
  1. #include "AddTileStackToPixmapcacheWorker.h"
  2.  
  3. #include <iostream>
  4. #include <QDebug>
  5. #include <QThread>
  6.  
  7. AddTileStackToPixmapcacheWorker::AddTileStackToPixmapcacheWorker (QString Filenames) :
  8. m_Filename (Filenames)
  9. {
  10. }
  11.  
  12. AddTileStackToPixmapcacheWorker::~AddTileStackToPixmapcacheWorker()
  13. {
  14. }
  15.  
  16. void AddTileStackToPixmapcacheWorker::run()
  17. {
  18. QPixmap ima;
  19. bool b = LoadImageDataRgb8(m_Filename, ima);
  20. }
  21.  
  22.  
  23.  
  24. bool AddTileStackToPixmapcacheWorker::LoadImageDataRgb8( QString fileNameRelative, QPixmap & ima )
  25. {
  26. if (fileNameRelative.size())
  27. {
  28. QString fileNameAbsolute = "D:/Sharing/MicroscopyImageData/falc_Nr2_13013792_2015.04.02-12.32.10/images/" + fileNameRelative;
  29. const char * pChar;
  30.  
  31.  
  32. //check ending for .jpg or .jpeg
  33. if (fileNameAbsolute.endsWith(".jpg", Qt::CaseInsensitive) || fileNameAbsolute.endsWith(".jpeg", Qt::CaseInsensitive))
  34. {
  35. pChar = "JPG";
  36. }
  37.  
  38. ima.load(fileNameAbsolute, pChar);
  39. return true;
  40. }
  41.  
  42. return false;
  43. }
To copy to clipboard, switch view to plain text mode 


I use it like that:
Qt Code:
  1. #include "AddTileStackToPixmapcacheWorker.h"
  2.  
  3. void myThreadingExample()
  4. {
  5.  
  6. std::vector<QString> vecStrings;
  7. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_0_Index_0_ds(1).jpg"));
  8. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_0_Index_0_orig.jpg"));
  9. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_1_Index_0_ds(1).jpg"));
  10. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_1_Index_0_orig.jpg"));
  11. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_10_Index_0_ds(1).jpg"));
  12. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_10_Index_0_orig.jpg"));
  13. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_11_Index_0_ds(1).jpg"));
  14. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_11_Index_0_orig.jpg"));
  15. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_12_Index_0_ds(1).jpg"));
  16. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_12_Index_0_orig.jpg"));
  17. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_13_Index_0_ds(1).jpg"));
  18. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_13_Index_0_orig.jpg"));
  19. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_14_Index_0_ds(1).jpg"));
  20. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_14_Index_0_orig.jpg"));
  21. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_15_Index_0_ds(1).jpg"));
  22. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_15_Index_0_orig.jpg"));
  23. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_16_Index_0_ds(1).jpg"));
  24. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_16_Index_0_orig.jpg"));
  25. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_17_Index_0_ds(1).jpg"));
  26. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_17_Index_0_orig.jpg"));
  27. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_18_Index_0_ds(1).jpg"));
  28. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_18_Index_0_orig.jpg"));
  29. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_19_Index_0_ds(1).jpg"));
  30. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_19_Index_0_orig.jpg"));
  31. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_2_Index_0_ds(1).jpg"));
  32. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_2_Index_0_orig.jpg"));
  33. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_20_Index_0_ds(1).jpg"));
  34. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_20_Index_0_orig.jpg"));
  35. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_21_Index_0_ds(1).jpg"));
  36. vecStrings.push_back(QString("img_Rev_1_Mag_2.5_Stack_21_Index_0_orig.jpg"));
  37.  
  38.  
  39. for (int i=0; i< vecStrings.size(); i++)
  40. {
  41. AddTileStackToPixmapcacheWorker *pAddTileStackToPixmapcacheWorker = new AddTileStackToPixmapcacheWorker(vecStrings[i]);
  42. pAddTileStackToPixmapcacheWorker->setAutoDelete(true);
  43. QThreadPool::globalInstance()->setExpiryTimeout(-1);
  44. QThreadPool::globalInstance()->start(pAddTileStackToPixmapcacheWorker);
  45. }
  46. }
  47.  
  48.  
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52. QApplication app(argc, argv);
  53. myThreadingExample();
  54. return app.exec();
  55. }
To copy to clipboard, switch view to plain text mode 

When I run this i get stuff like the following in the console:
Qt Code:
  1. QObject::startTimer: Timers cannot be started from another thread
  2. QObject::startTimer: Timers cannot be started from another thread
  3. QObject::startTimer: Timers cannot be started from another thread
  4. QObject::startTimer: Timers cannot be started from another thread
  5. QObject::startTimer: Timers cannot be started from another thread
  6. QObject::startTimer: Timers cannot be started from another thread
  7. QObject::startTimer: Timers cannot be started from another thread
  8. QObject::startTimer: Timers cannot be started from another thread
To copy to clipboard, switch view to plain text mode 

and when I close my console window I get:
Qt Code:
  1. QObject::~QObject: Timers cannot be stopped from another thread
  2. QWaitCondition: Destroyed while threads are still waiting
  3. QWaitCondition: Destroyed while threads are still waiting
  4. QWaitCondition: Destroyed while threads are still waiting
  5. QWaitCondition: Destroyed while threads are still waiting
  6. QWaitCondition: Destroyed while threads are still waiting
  7. QWaitCondition: Destroyed while threads are still waiting
  8. QWaitCondition: Destroyed while threads are still waiting
  9. QWaitCondition: Destroyed while threads are still waiting
To copy to clipboard, switch view to plain text mode 

Once or twice (out of around 20 or 30 tries) it also crashed with some unhandled exception, but I don't remember what the exact error description was.

What am I doing wrong?
How else would I be able to load several images asynchronously?!?
I've tried to use only one "extra" thread for loading images, which is more stable, but for my use case is also much slower than the example in this post. So, to summarize: I am looking for either:
1) a way to fix my QRunnable example or
2) a different multithreaded approach that works
Any ideas?!? Many many thanks in advance!!!!!