ok.. this is what I done so far.. it took me longer as the first approach (query executed directly by the GUI trough a signal) was working without problems in Linux but randomly crash in Windows..
Does the following looks as a good approach or it is advisable to be done in an other way??
I have three classes involved, a MainWindow (QMainWindow derived), ThreadManager (QThread derived) and MainProgram (independent class with the model code).
MainWindow starts the thread calling QThread::start().
The QThread::start() call really executes the code in the ThreadManager::run() function that contain MainProgram::run() with all the model code.
The pause and stop system is implemented with the bool ThreadManager::running and bool ThreadManager::stopped variables.
Often (every few seconds) the model code goes to check the status of that variables that can be changed also from the GUI. The code is:
void
ThreadManager::refreshGUI(){
checkQuery(0,0,false);
while (!running){
if(stopped){
break;
}
}
if (stopped){
emit upgradeLogArea("Model has been stopped.");
running= false;
throw(2);
}
}
void
ThreadManager::refreshGUI(){
checkQuery(0,0,false);
while (!running){
if(stopped){
break;
}
}
if (stopped){
emit upgradeLogArea("Model has been stopped.");
running= false;
throw(2);
}
}
To copy to clipboard, switch view to plain text mode
checkQuery(int, int, bool) is a function that can be called both from the GUI trough a signal or from the running thread trough refreshGUI(), and it is so protected with a QMutex.
It's role is to change/control the status of the query parameters that are members of ThreadManager. The third function parameter is a bool that tell the function if the query parameters should be set (if the query come from the GUI) or if the query should be executed and its results sent back to the GUI with a signal (if the call come from the running thread).
The code is :
void
ThreadManager::checkQuery(int px_ID, int currentLayerIndex, bool newRequest){
if(newRequest){
pxQueryID = px_ID;
layerQueryPos = currentLayerIndex;
if(stopped){computeQuery(pxQueryID, layerQueryPos);layerQueryPos = -1;} // model is stopped, no way the model thread will do the query work
else{emit publishQueryResults("<i>..wait.. processing query..</i>");} // model is running.. it will be the model thread to execute the query
return;
} else {
if(layerQueryPos<0){
return;
} else {
computeQuery(pxQueryID, layerQueryPos);
layerQueryPos = -1;
return;
}
}
}
void
ThreadManager::checkQuery(int px_ID, int currentLayerIndex, bool newRequest){
QMutexLocker locker(&mutex);
if(newRequest){
pxQueryID = px_ID;
layerQueryPos = currentLayerIndex;
if(stopped){computeQuery(pxQueryID, layerQueryPos);layerQueryPos = -1;} // model is stopped, no way the model thread will do the query work
else{emit publishQueryResults("<i>..wait.. processing query..</i>");} // model is running.. it will be the model thread to execute the query
return;
} else {
if(layerQueryPos<0){
return;
} else {
computeQuery(pxQueryID, layerQueryPos);
layerQueryPos = -1;
return;
}
}
}
To copy to clipboard, switch view to plain text mode
The whole code is located at:
http://www.regmas.org/doc/referenceM...ainWindow.html
http://www.regmas.org/doc/referenceM...adManager.html
http://www.regmas.org/doc/referenceM...inProgram.html
Thank you.. I think I finally found a pleace to discuss the Qt programming.. the mailing list looks too technical for a beginner like me... 
Cheers...
Sylvaticus
Bookmarks