Question

I know that it's bad to run any kind of GUI widget from within a separate thread. For just messages, this can be overcome with signals to the main thread. But what if the thread needs a user input, how can the answer be signaled back to the thread and how can that thread wait for that answer?

My particular case is an application that uses sftp from libssh. During connection and authentication, the user may need to answer one or more questions. But for performance reasons, all the SSH/SFTP stuff must be running in a separate thread.

Was it helpful?

Solution

It is not possible to use GUI classes in non-GUI threads at all. What you can do is use signals and slots to exchange information from one thread to another. Send a signal from your worker thread and wait on a semaphore, like QWaitCondition. Send a message back with the answer.

In your case you could also use Qt::BlockingQueuedConnection as connection type to stop your worker thread until the user has entered data. QInputDialog also waits until the user has finished entering data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top