簡體   English   中英

在QThread中運行較長的操作並將信號發送到主線程仍會凍結UI

[英]Running a long operation in QThread and sending signals to the main thread still freezes the UI

這就是我所擁有的:

void MyThread::run() {
  int progress = 0;
  while (1) {
    // do something
    progres++;
    emit(progressChanged(progress));
  }
}

// mainwindow
auto t = new MyTread();
connect(t, SIGNAL(progressChanged(int)), this, SLOT(onProgressChanged(int)));
t->start();

void MaiWindow::onProgressChanged(int) {
  this->progressBar->setValue(progressBar->value() + 1);
}

它可以正常工作,線程中的工作已完成,進度條一直上升到100%。

但是 ,UI完全凍結/緩慢。 拖動帶有進度欄的窗口會導致5秒鍾的延遲。 我嘗試使用較低的線程優先級-沒有結果。

也許我這里需要互斥鎖?

不要發出太多的progressChanged信號。 信號速度很快,但是如果您每秒設置數百或數千次進度條值,則UI會凍結。 保持進度欄更改為最小,每秒5-10次更改已足夠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM