私はC++ QT5 Widgetデスクトップアプリケーションを作成していますが、スタート/ストップボタンを押すと別のスレッドで時間のかかる操作MainWindow::performLengthyOperation(bool)
を実行する必要があります。別のスレッドでQT MainWindowメソッドを実行する
この時間がかかる操作は、私のMainWindow.h/cppでかなり長いメソッドです。バックグラウンドIOアクティビティを停止する操作には約6秒かかり、開始には約2秒かかります。開始/停止ボタンが押されている間、UIは応答しません。基本的には、私のボタンクリックイベントに接続されたスロットでは、以下のロジックを実行する必要があります。私はfollowing article出くわしこれを実行する方法の例を探していたが、私はそれはそのUIにアクセスできるように何とか労働者にメインウィンドウを取得する必要がありますように私は私のシナリオにそれを適応問題を抱えています
void
MainWindow::on_pushButtonStart_clicked(bool checked)
{
// temporarily disable the pushbutton
// until the lengthy operation completes
mUI->pushButtonStart->setEnabled(false);
if (checked) {
// Start the timer tick callback
mTickTimer.start(APP_TICK, this);
mUI->pushButtonStart->setText("starting...");
// This method needs to somehow run in its own QT thread
// and when finished, call a slot in this MainWindow to
// re-enable the pushButtonStart and change the statusBar
// to indicate "runing..."
performLengthyOperation(true);
//mUI->pushButtonStart->setText("Stop")
//mUI->statusBar->setStyleSheet("color: blue");
//mUI->statusBar->showMessage("runing...");
} else { // Stop the protocol threads
// Stop the subsystem protocol tick timer
mTickTimer.stop();
mUI->pushButtonStart->setText("stopping...");
// This method needs to somehow run in its own QT thread
// and when finished, call a slot in this MainWindow to
// re-enable the pushButtonStart and change the statusBar
// to indicate "ready..."
performLengthyOperation(false);
// finally toggle the UI controls
//mUI->pushButtonStart->setText("Start")
//mUI->statusBar->setStyleSheet("color: blue");
//mUI->statusBar->showMessage("ready...");
}
}
ウィジェットなどとそれは少し過剰なようだ。
これらの時間を要する操作(パラメータとしてメインウィンドウを渡すこと)が可能なラムダ関数を非同期で実行する簡単な方法を理想的に探しています。それはQThreadsを使用し、オブジェクトをスレッドなどに移動する方が望ましいでしょう。しかし、QTフレームワークがこれが安全で可能なものかどうかを知るには十分ではありません。 std ::非同期とラムダを使用して
ウィンドウを渡しても、その他のスレッドから操作することはできません。これらの操作をメインスレッドにマーシャリングする必要があります(ただし、Qtはシグナル/スロットでこれを行うことができます)。 – Steve