1

ボタンをクリックするたびに状態を挿入したいループ内に無限ループがあり、現在のループが中断されます。私は次のようないくつかの方法を試しました:

if(ui->btnStop->isDown())
  {
     break;
  }


if(ui->btnStop->isChecked())
      {
         break;
      }

if(cv::waitKey(10)>=0)
{
    break;
}

しかし、うまくいきません。cv::waitKey が Qt で機能しないのはなぜかと思いますが、Qt 以外のプロジェクトでは問題なく機能します。QPushButton で無限ループを中断する他の方法はありますか? どんな助けでも大歓迎です。

4

1 に答える 1

3

実行がループ内でロックされている間はイベント プロセッサを実行できないため、機能しません。最も簡単な解決策は、単純QApplication::processEvents()に各ループで呼び出すことです。これにより、イベント プロセッサが強制的に実行されます。

//  Add a boolean to your class, and a slot to set it.
MyClass
{
    ...
private slots:
    void killLoop() { killLoopFlag_ = true; }

private:
    bool killLoopFlag_;
}

// In the constructor, connect the button to the slot.
connect( ui->btnStop, SIGNAL( clicked() ),
         this, SLOT( killLoop ) );

//  Then when performing the loop, force events to be processed and then
//  check the flag state.
killLoopFlag_ = false;
while ( true ) {
    //  ...Do some stuff.
    QApplication::processEvents();
    if ( killLoopFlag_ ) {
        break;
    }
}

ただし、自問する必要があります: GUI スレッド内で長時間実行する計算を行う必要がありますか? 答えは通常ノーです。

于 2013-03-17T07:59:09.013 に答える