我正在做一些QThread重新實現的工作。偶爾,我想問用戶一個是/否的問題,所以我打算使用QMessageBox :: question()。問題是,我無法從線程調用它。這不是一個大問題,我可以發出一個信號,連接到主GUI線程中的一個插槽,該插槽將顯示消息框,但我還需要自定義線程來阻止並等待消息框被解除並檢索返回值(以及QMessageBox :: StandardButton)。我該如何解決這個問題?如何使我的QThread塊,並等待從主線程調用的函數返回一個值?
編輯: 請問下面的(僞)代碼是否有效?
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();
}
該文件完全混淆了我。 :(你可以看看編輯中的僞代碼並與之相關嗎?謝謝。 – neuviemeporte