Then you need to wait for the response from the gui thread before you continue. You probably have a list of files to work through, so just keep the index and resume from where you left off when you get the appropriate signal from the main thread.
Then you need to wait for the response from the gui thread before you continue. You probably have a list of files to work through, so just keep the index and resume from where you left off when you get the appropriate signal from the main thread.
Or use the signal semantics that will halt the execution of the worker thread until all the slots are executed.
This works, thanks! ...but the return value is always the same!?
Slot in GUI thread:
In copy thread:Qt Code:
return QMessageBox::question(this, tr("Override?"), tr("File '%1' already exists! Override?").arg(fileName), QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll); }To copy to clipboard, switch view to plain text mode
Qt Code:
signals: (...) (...) int result = emit overrideQuestion(fileInfo.fileName());To copy to clipboard, switch view to plain text mode
result is always "267386880"!?
Ok, but how can I do this with an int return value?
Sorry, I don't understand it!Variable as return value???
The slot will not return anything. You either need to store the variable elsewhere or you need to send a signal to the thread to indicate the return value. I would use a state machine in the thread and use a signal to indicate the return value (ie, the thread would change to paused state when the notification is raised and resume upon receiving the response from the user).
You might have guessed that I hate blocking code, so everything runs via events and signals
I've written a file manager before that goes one step further - if it needs a response from the user (eg. overwrite question), it sends a signal to the main thread to request about that file and processes the other files in the background, returning to the other files if and when a response is received from the main thread about those files.
Last edited by squidge; 23rd May 2011 at 17:30.
store variable elsewhere .. can you give me an example with my above code please!?
eg. you store the result from the message dialog in a variable that both threads have access it. I don't think you really need an example for that.
But I would say that the method I described above is a better idea and keeps encapsulation.
Ok, but how can I paused the worker thread?
You don't "pause" it, you simply return to your threads event loop waiting for something to happen (such as a signal from the main ui to continue). When you receive this signal, you continue, using the context information you stored in your thread when you returned to the event loop.
I assume you store your filenames in a vector or similar, so all you need to do is store an iterator to the element you were parsing when you emitted the message box request, then you restart from there once you receive the information back from the main thread.
Bookmarks