for循环中的qt-qthread

for循环中的qt-qthread,qt,for-loop,qthread,Qt,For Loop,Qthread,我在尝试运行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();//

我在尝试运行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.

}
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

    }
connect(&myworkingthread)、信号(updateaprocess(int))、进程条2、插槽(setValue(int));
for(int i=0;isetValue(i);//processbar用于显示处理过程
//此代码的问题:数据完全错误,因为下一个线程将在最后一个线程完成之前启动。
}
我还想展示我的WorkingRead流程,该流程应该由 信号和插槽。如果我使用上面的代码,数据是完全错误的。因为第二个线程将在第一个线程完成之前启动

然后我更改代码如下:

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.

}
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

    }
connect(&myworkingthread)、信号(updateaprocess(int))、进程条2、插槽(setValue(int));
for(int i=0;isetValue(i);//processbar用于显示处理过程
}
这段代码的问题是,在for循环遍历所有文件之前,线程的processbar不起作用

有没有办法使线程进程处于for循环中?

您需要调用

QApplication::processEvents()


据我记忆所及,这应该在线程中的“emit”行之后调用…

Progressbar(小部件)只能使用GUI线程更新我使用信号和插槽机制在之前进行更新,但不使用for循环。可以完成。有用的链接..我不了解您的问题。您是否遵循了另一个线程中关于myworkingthread的建议,即正确发出信号?先调用start(),然后调用wait()使主线程阻塞,从而使使用另一个线程变得毫无意义。