QThread の再実装でいくつかの作業を行っています。ときどき、ユーザーに Yes/No の質問をしたいので、QMessageBox::question() を使用することを計画していました。問題は、スレッドから呼び出すことができないことです。それは大きな問題ではありません。メッセージ ボックスを表示するメイン GUI スレッドのスロットに接続する信号を送信できますが、カスタム スレッドをブロックして、メッセージ ボックスが閉じられるのを待ち、戻り値を取得する必要もあります。値も同様です (ここでは、QMessageBox::StandardButton)。どうすればそれを行うことができますか?
編集: 次の(疑似)コードでうまくいきますか?
class MyThread
{
public:
MyThread(QObject *parent)
{
connect(this, SIGNAL(inputRequired()), parent, SLOT(popMsgBox()), Qt::QueuedConnection);
}
void MyThread::run()
{
QMutex m;
for (...)
{
if (something)
{
m.lock();
emit inputRequired();
w.wait(&m);
m.unlock();
}
if (MyGui->ans_ == Yes) do_something();
}
}
signals:
void inputRequired();
protected:
QWaitCondition w;
};
void MyGui::popMsgBox()
{
ans_ = QMessageBox::question(this, "Question", "Yes or no?", Yes | No);
MyThread->w->wakeAll();
}