-2

forループで作業スレッドを実行しようとすると問題が発生しました。

私のコードは次のようなものです:

connect(&myworkingthread,SIGNAL(updataprocess(int)),processbar2,SLOT(setValue(int)));

    for (int i=0;i<n;i++){

      // each individual data will be loaded in this part...

      myworkingthread.start();// this thread will take 5 secs to finish, a signal is 
    // also emitted to show the process of this thread(processbar2).

     // after working thread, the processed data will be saved... 

      processbar1->setValue(i); // processbar is used to show the processing process

      //problem of this code: data is totally wrong because the next thread will start before the last one finish.

}

また、シグナルとスロットによって実装されるはずの myworkingthread のプロセスも示したいと思います。上記のコードを使用すると、データが完全に間違っています。最初のスレッドが終了する前に 2 番目のスレッドが開始されるためです。

次に、コードを次のように変更します。

connect(&myworkingthread,SIGNAL(updataprocess(int)),processbar2,SLOT(setValue(int)));
    for (int i=0;i<n;i++){

          // each individual data will be loaded in this part...

          myworkingthread.start();// this thread will take 5 secs to finish
          // signal is also emitted to show the process of this thread(processbar2).
          myworkingthread.wait();// i will wait the thread until it finish

         // after working thread, the processed data will be saved... 

          processbar1->setValue(i); // processbar is used to show the processing process

    }

このコードの問題は、for ループがすべてのファイルを通過するまで、スレッドのプロセスバーが機能しないことです。

forループでスレッドプロセスを作成する方法はありますか?

4

1 に答える 1

0

電話する必要があります

QApplication::processEvents();

QProgressDialog を使用した簡単な例を参照してください: これが正しく機能しない理由はありますか? 私が覚えている限り、これはスレッドの「発行」行の後に呼び出す必要があります...

于 2013-09-08T16:25:48.740 に答える