Real-time refresh of QT interface under multi-threading
Problem phenomenon
Main interface: 4 Label controls, used to display video pictures, but for the smooth operation of the main thread, four threads are used to collect video streams, and directly set the picture to display on the label.
VIDEOlabel->setPixmap(QPixmap::fromImage(imgScaled));
Later, after running, I found that the interface video can run normally at the beginning and the display is very smooth. The video display freezes after about 2min. Sometimes only one label is refreshing normally, sometimes all 4 are not refreshed, but you can minimize the window or click the interface. The button in switch to the next interface and then back to the interface will refresh normally, but it won’t last long.
Solutions tried
- Reference blog: Only call update() in the main thread. After this method is used, if the calling frequency is less than 40ms, the program will crash sporadically, but the problem cannot be solved. The actual interface keeps refreshing for a longer time.
VIDEOlabel->repaint();
Way, no way.- Reference blog: Rewrite the showEvent function , the actual measurement is useless.
But the final processing all points to: the object operations of the UI interface are only executed in the main thread.
Final treatment
In the thread, the latest image collected is sent to the main thread for execution through signals and slots
emit UpdateWidgetSignalx(pixmap);/**< 发送更新控件信号*/
Receive the signal in the main thread and call the slot function to process it
/**
* @brief MainWindow::slotUpdateWidgetx
* @param winindex
* @param VideoWindow
*/
void MainWindow::slotUpdateWidgetx(quint8 winindex ,QPixmap &VideoWindow)
{
if(winindex <= 0)
{
return;
}
char windowstr = 'A'+static_cast<char>(winindex)-1;
switch(windowstr)
{
case 'A':
VIDEOAlabel->setPixmap(VideoWindow);
break;
case 'B':
VIDEOBlabel->setPixmap(VideoWindow);
break;
case 'C':
VIDEOClabel->setPixmap(VideoWindow);
break;
case 'D':
VIDEODlabel->setPixmap(VideoWindow);
break;
default:
break;
}
}