I have a worker object on a thread that is processing files. In some cases, the file needs user input to continue. So I need my worker to block until the user gives the input, and then it can continue.
{
Q_OBJECT(Worker)
public:
Worker();
~Worker();
protected slots:
void process()
{
...
if ( needInputFromUser )
emit signal_openDialog();
// Continue with processing
...
}
signals:
signal signal_openDialog();
}
class Worker : public QObject
{
Q_OBJECT(Worker)
public:
Worker();
~Worker();
protected slots:
void process()
{
...
if ( needInputFromUser )
emit signal_openDialog();
// Continue with processing
...
}
signals:
signal signal_openDialog();
}
To copy to clipboard, switch view to plain text mode
class OnTheGUIThread : public QWindow
Q_OBJECT(OnTheGUIThread)
public:
OnTheGUIThread();
~OnTheGUIThread();
public:
void whereWorkerIsStarted()
{
...
Worker *worker = new Worker;
connect(thread,SIGNAL(started()),worker,SLOT(process()));
connect(worker,SIGNAL(signal_quit()),thread,SLOT(quit()));
connect(worker,SIGNAL(signal_openDialog()),this,SLOT(slot_openDialog()),Qt::BlockingQueuedConnection);
thread->start();
...
}
protected slot:
void slot_openDialog()
{
dialog.exec(); // Blocks until window closed;
worker.setData(dialog.data());
return;
}
class OnTheGUIThread : public QWindow
Q_OBJECT(OnTheGUIThread)
public:
OnTheGUIThread();
~OnTheGUIThread();
public:
void whereWorkerIsStarted()
{
...
QThread *thread = new QThread;
Worker *worker = new Worker;
connect(thread,SIGNAL(started()),worker,SLOT(process()));
connect(worker,SIGNAL(signal_quit()),thread,SLOT(quit()));
connect(worker,SIGNAL(signal_openDialog()),this,SLOT(slot_openDialog()),Qt::BlockingQueuedConnection);
thread->start();
...
}
protected slot:
void slot_openDialog()
{
dialog.exec(); // Blocks until window closed;
worker.setData(dialog.data());
return;
}
To copy to clipboard, switch view to plain text mode
If I connect the openDialog signal/slots with a Qt::BlockingQueuedConnection, I get a deadlock. According to the documentation:
Same as QueuedConnection, except the current thread blocks until the slot returns. This connection type should only be used where the emitter and receiver are in different threads.
They are in different threads (or are they?), and I want the current thread to block until the slot returns. Am I just misunderstanding what it's used for?
Secondary question, what's the best way to pass data back to the processing thread?
Bookmarks